var length = 10;
function fn() {
console.log( this.length ); // 10
}
var obj = {
length: 5,
method: function ( fn ) {
fn(); // 10 前面没有引导对象,是函数调用模式
arguments[ 0 ](); // 2
}
};
obj.method( fn, 1 ); // 打印 10 和 2
想要知道为什么,就是看看运行的时候 this 是谁就可以了。
第一个 fn(); 没有指定this,默认是全局对象。 如果是浏览器里面 就是 window.length
第二个 this,指向 arguments 本身,所以输出的是 arguments.length
所以就是 10 和 2 了