Reputation: 18895
I have a javascript file that is attempting to reference a previously defined value, but the container of the value is null:
if (typeof (myNamespace) == "undefined") { myNamespace = {}; }
myNamespace.myClass = {
myConstants: {
value: "someValue",
},
something : {
values: [myNamespace.myClass.myConstants.value] // Errors here
}
};
I thought JavaScript executed in a top down manner, so the myNamespace.myClass.myConstants.value
should be already be defined when the something.values[]
is getting populated.
Upvotes: 1
Views: 18
Reputation: 413737
At the point your expression myNamespace.myClass.myConstants.value
is evaluated, the value of myNamespace.myClass
is still undefined
. Until the whole object initializer expression is evaluated, the assignment doesn't happen.
The upshot of this is that there's no way to make internal cross-references from inside an object initializer expression. You have to split it out into a separate assignment.
myNamespace.myClass = {
myConstants: {
value: "someValue",
},
something : {
values: []
}
};
myNamespace.myClass.something.values.push(myNamespace.myClass.myConstants.value);
Upvotes: 3