首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JS base knowledge (四) this

2012-11-22 
JS base knowledge (4) thisthis始终指向当前方法的调用者如果方法调用签名的左边有对象,则this指向该对象

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

function foo(){console.log(this); }foo();// print windowvar 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

热点排行