I’ve create a namespace like the one below.
var Constants = {
Foo: 'bar',
SomeVar: {
key: 'value'
}
};
What I want to do is:
var Constants = {
Foo: 'bar',
SomeVar: {
key: Constants.Foo + 'value' // Notice the Constants.Foo
}
};
What I want to do is be able to use the value of Constants.Foo to create a value for SomeVar. However this throws an error in Firebug.
So I’m wondering, how do I access the value of Foo inside the Constants namespace; and also, I’ve been reading a few articles on encapsulation in Javascript (like this one), is it possible to access value of Foo without having to do Constants.Foo? Perhaps some sort of way to preserve a reference to the namespace’s this keyword and then I can do something like thisParent.Foo.
Thanks!
EDIT:
This is the error I’m getting by the way:
TypeError: Constants is undefined
That’s not possible. An object literal doesn’t have a name until you assign it to something, and by then, it’s too late. Object literals aren’t functions, either, so there’s no contextual keyword you could use;
thiswould still be whatever it was in the containing function. Adding the property afterwards is the only way.And no, a namespace is not a thing in JavaScript. That is an object. Nothing more.