var a={b:44,c:this.b+1};
I want c
to be b+1
or 45 in this case. but when I try this, I get Cannot read property 'b' of undefined
error.
var a={b:44,c:this.b+1};
I want c
to be b+1
or 45 in this case. but when I try this, I get Cannot read property 'b' of undefined
error.
In JavaScript, when creating an object, the execution context doesn't change. Hence, the this
value is picked up from the top-level in your case. Or to articulate in a different way, you can't access the properties of the object if it's not finished initializing yet.
The actual solution to your problem might be:
var a = { b: 44 };
a.c = a.b + 1;