I have this JavaScript code:
var r = {
a1:function() {
alert('hey!');
},
a2:this.a1
/*,
a3:r.a1, //<--Make an error when running
a4:a1 //<--Make an error when running
*/
};
When executing r.a1() I get an alert but when executing r.a2() I get message:
TypeError: r.a2 is not a function
Why is it? How can I make this work in in one statement?
this, in your definition does not refer to r, but to the actual context (probablywindow)you should define it like this: