i have a little code snippet below
var foo = {
bar: function () {
return this.baz;
},
baz: 1
};
(function () {
return typeof arguments[0]();
})(foo.bar);
baz = 1;
//result undefined
when the foo.bar function is executed, this refers to the window scope which of course knows nothing about baz so i have defined baz=1 in window. but the program still not working and returning undefined. why it is returning undefined while baz have defined in window and i am executing foo.bar from window
bar: function () { console.log(this); return this.baz; }you can see thatthisrefers to its parent, which isarguments.