So I have this JavaScript code:
var obj={
a: “Chris”,
b: this
};console.log(obj.b); //{}
The console.log return an empty object rather than the object with variable a and b.
Anyone know why?
Thank you very much!
So I have this JavaScript code:
var obj={
a: “Chris”,
b: this
};console.log(obj.b); //{}
The console.log return an empty object rather than the object with variable a and b.
Anyone know why?
Thank you very much!
If this code is in the global scope, which I assume it is, this
points to the global object (whatever it is). If you want it to point to the object itself, you cannot do it in the literal, you need to assign it later (after you create the object itself).