javascriptecmascript-6javascript-objectsjavascript-namespaces

How to have nested object inside namespace variable?


I am programming using namespaces, and so far I have only used variables and functions inside it. I tried to do a nested object in it, but it doesn't seem to work like I expect it. Here's the code

const myNameSpace = {

    someVar: '',
    otherVar: '',

    someFunc() {
        // code here
    },

    nestedObject: {
        param1: '',
        param2: '',
        param3: ''
    }
};

other variables work fine, but when I try to access param1, like this

myNameSpace.nestedObject.param1

it returns undefined, rather than an empty string like I have declared it. Also accessing only myNameSpace.nestedObject returns an empty string...

What is wrong with this, and is it even possible what I want?


Solution

  • It is working as expected, Run following code snippet.

    const myNameSpace = {
    
        someVar: '',
        otherVar: '',
    
        someFunc() {
            // code here
        },
    
        nestedObject: {
            param1: 'param 1 value',
            param2: 'param 2 value',
            param3: 'param 3 value'
        }
    };
    
    console.log(myNameSpace.nestedObject.param1);