日期:2014-05-16  浏览次数:20301 次

JS base knowledge (4) this
this始终指向当前方法的调用者
如果方法调用签名的左边有对象,则this指向该对象。如果无,则默认指向window.

function foo(){
console.log(this); 
}

foo();// print window

var bar = {name:"bar"};
bar.f1 = function(){
console.log(this); 
}
bar.f1();// print bar's value.


this因事件加载方式而不同:
inline, 和microsoft model是指向Handler。所以运行时,handler的this指向了window对象。

通过apply/call方法在运行时改变this的值:
apply(this, array); call(this, param1, param2);
参考:
http://www.quirksmode.org/js/this.html