总共有以下几种情况。。(都是没有任何bind或call处理或严格模式下)在非严格模式下,方法执行环境的默认this,都指向当前的window对象。
1 某个对象的某个方法,比如dom.onclick 中的this都是dom对象自己
dom.onclick(function(){
this.name;
})
2.js执行方法 在非严格模式下,this都会指向当前全局window
function test(){
console.log(this);//window 严格模式下为undefined
}
3.比较有迷惑性的
function Super(){
var Method = function(){
console.log(this);//很多人都会认为这里会指向当前这个Super对象,但是这里会指向window。在严格模式下会undefined
}
Method()
}
new Super();
4.也可以改变this对象,比如用call或者apllay
var a = Function.prototype.call.apply(function(a){console.log(this);return a;}, [0,4,3]);alert(a);
//此时里面的this是数字0,a为数字4.。
//解释 func.apply(obj,args) 等价于 obj.func(args)
所以原方法就等价于 <pre name="code" class="javascript">function(a){console.log(this);return a;}.call(0,4,3),相当于 0是this,4和3是参数
</pre><pre name="code" class="javascript">