function a(){
console.log(window == this) //true
}
a()
上面代码中当函数被调用时this的值是全局对象,在浏览器中,就是window对象
使用.call与.play方法可以改变this的值
function a(){
this.a=='b';
}
console.log(a.call({a:'b'})) //true
function foo(a,b){
console.log(a=='1');
console.log(b=='2');
}
foo.apply(this,[1,2]); //true
foo.call(this,1,2); //true