You are trying to reference a property that is defined via an object literal, from within the object literal itself. This is not possible. The properties are assigned to the str
variable only after the entire object literal is evaluated. So, within the object literal, you cannot refer to properties of str
.
So, when you assign an object literal to a variable,
var obj = { ... };
then, inside that object literal, you cannot use the obj
reference at all. At the time the object literal is evaluated, obj
is still undefined
, and only after the object literal has been fully parsed and evaluated, the corresponding Object value is created, and assigned to obj
.
var obj = {
a: 123,
b: obj.a // this will throw, "obj" is undefined
};
obj.b = obj.a; // this works