I have a JS object property defined in an object literal:
reqHeader: [{name:'Chris'},{age:'06'}]
which I am nesting inside another property in the same object literal:
content: {headers:
{reqHeader: this.reqHeader}
},
Now when I try to access this from a method in the same object literal, it says it is undefined:
getHeaders: function(){
var a = this.content['headers']['reqHeader'];
alert(a);
}
Full code: http://jsfiddle.net/Amnesiac/zZP83/5/
Thanks,
Chris.
That won’t work, because
thisis not a reference to that object. That is, it is not the case that JavaScript setsthisto refer to an object that’s “under construction” inside the object literal block. It remains set to whatever it is outside that expression.What you can do is something like: