arguments[0]()
is executing in the Window context, but it does not have any object property defined in the name bar (which is a property of foo). This is why you get undefined
. To solve the issue, you can bind the object.
Change
foo.bar
To
foo.bar.bind(foo)
var foo = {
bar: function() { return this.baz; },
baz: 1
};
var a = (function(){
console.log(this.constructor.name); // Window
return typeof arguments[0]();
})(foo.bar.bind(foo));
console.log(a);
6
solved Kindly explain why in following example the output is ‘undefined’? [duplicate]