<script> var foo = { bar: function() { return this.baz; }, baz: 1 }; var a = (function(){ return typeof arguments[0](); })(foo.bar); console.log(a); </script>
注意:我已经通过了以下链接,但它并不能解释这一点 例子。这里没有构造函数。。。 How to access the correct `this` inside a callback?
arguments[0]() 正在执行 Window 上下文,但它没有在名称中定义任何对象属性 (属于 福 ). 这就是为什么你 undefined
arguments[0]()
undefined
改变
foo.bar
到
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);