本文摘自PHP中文网,作者青灯夜游,侵删。
this的中文意思为“当前;这个”,是javascript中的一个指针型变量,它指向当前函数的运行环境。在不同的场景中调用同一个函数,this的指向会发生变化,但它永远指向其所在函数的真实调用者;如果没有调用者,this就指向window。
本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
JavaScript 函数的作用域是静态的,但是函数的调用却是动态的。由于函数可以在不同的运行环境内执行,因此 JavaScript 在函数体内定义了 this 关键字,用来获取当前的运行环境。
this 是一个指针型变量,它指向当前函数的运行环境。
在不同的场景中调用同一个函数,this的指向也可能会发生变化,但是它永远指向其所在函数的真实调用者(谁调用就指向谁);如果没有调用者,this就指向全局对象window。
使用 this
this 是由 JavaScript 引擎在执行函数时自动生成的,存在于函数内的一个动态指针,指代当前调用对象。具体用法如下:
如果 this 未包含属性,则传递的是当前对象。
this 用法灵活,其包含的值也是变化多端。例如,下面示例使用 call() 方法不断改变函数内 this 指代对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var x = "window" ;
function a () {
this .x = "a" ;
}
function b () {
this .x = "b" ;
}
function c () {
console.log(x);
}
function f () {
console.log( this .x);
}
f();
f.call(window);
f.call( new a());
f.call( new b());
f.call(c);
|
下面简单总结 this 在 5 种常用场景中的表现以及应对策略。
1. 普通调用
下面示例演示了函数引用和函数调用对 this 的影响。
1 2 3 4 5 6 7 8 9 10 11 12 | var obj = {
name : "父对象obj" ,
func : function () {
return this ;
}
}
obj.sub_obj = {
name : "子对象sub_obj" ,
func : obj.func
}
var who = obj.sub_obj.func();
console.log(who.name);
|
如果把子对象 sub_obj 的 func 改为函数调用。
1 2 3 4 | obj.sub_obj = {
name : "子对象sub_obj" ,
func : obj.func()
}
|
则函数中的 this 所代表的是定义函数时所在的父对象 obj。
1 2 | var who = obj.sub_obj.func;
console.log(who.name);
|
2. 实例化
使用 new 命令调用函数时,this 总是指代实例对象。
1 2 3 4 5 6 7 | var obj = {};
obj.func = function () {
if ( this == obj) console.log( "this = obj" );
else if ( this == window) console.log( "this = window" );
else if ( this .contructor == arguments.callee) console.log( "this = 实例对象" );
}
new obj.func;
|
3. 动态调用
使用 call 和 apply 可以强制改变 this,使其指向参数对象。
1 2 3 4 5 6 7 8 9 10 11 | function func () {
if ( this .contructor == arguments.callee) console.log( "this = 实例对象" );
else if ( this == window) console.log( "this = window对象" );
else console.log( "this == 其他对象 \n this.constructor =" + this .constructor);
}
func();
new func();
cunc.call(1);
|
在上面示例中,直接调用 func() 时,this 代表 window 对象。当使用 new 命令调用函数时,将创建一个新的实例对象,this 就指向这个新创建的实例对象。
使用 call() 方法执行函数 func() 时,由于 call() 方法的参数值为数字 1,则 JavaScript 引擎会把数字 1 强制封装为数值对象,此时 this 就会指向这个数值对象。
4. 事件处理
在事件处理函数汇总,this 总是指向触发该事件的对象。
1 2 3 4 5 6 7 8 9 10 11 | <input type= "button" value= "测试按钮" />
<script>
var button = document.getElementsByTagName( "put" )[0];
var obj = {};
obj.func = function () {
if ( this == obj) console.log( "this = obj" );
if ( this == window) console.log( "this = window" );
if ( this == button) console.log( "this = button" );
}
button.onclick = obj.func;
</script>
|
在上面代码中,func() 所包含的 this 不再指向对象 obj,而是指向按钮 button,因为 func() 是被传递给按钮的事件处理函数之后才被调用执行的。
如果使用 DOM2 级标准注册事件处理函数,程序如下:
1 2 3 4 5 | if (window.attachEvent) {
button.attachEvent( "onclick" , obj.func);
} else {
button.addEventListener( "click" , obj.func, true );
}
|
在 IE 浏览器中,this 指向 window 对象和 button 对象,而在 DOM 标准的浏览器中仅指向 button 对象。因为,在 IE 浏览器中,attachEvent() 是 window 对象的方法,调用该方法时,this 会指向 window 对象。
为了解决浏览器兼容性问题,可以调用 call() 或 apply() 方法强制在对象 obj 身上执行方法 func(),避免出现不同的浏览器对 this 解析不同的问题。
1 2 3 4 5 6 7 8 9 | if (window.attachEvent) {
button.attachEvent( "onclick" , function () {
obj.func.call(obj);
});
} else {
button.attachEventListener( "onclick" , function () {
obj.func.call(obj);
}, true );
}
|
当再次执行时,func() 中包含的 this 始终指向对象 obj。
5. 定时器
使用定时器调用函数。
1 2 3 4 5 6 7 8 | var obj = {};
obj.func = function () {
if ( this == obj) console.log( "this = obj" );
else if ( this == window) console.log( "this = window对象" );
else if ( this .constructor == arguments.callee) console.log( "this = 实例对象" );
else console.log( "this == 其他对象 \n this.constructor =" + this .constructor);
}
setTimeOut(obj.func, 100);
|
在 IE 中 this 指向 window 对象和 button 对象,具体原因与上面讲解的 attachEvent() 方法相同。在符合 DOM 标准的浏览器中,this 指向 window 对象,而不是 button 对象。
因为方法 setTimeOut() 是在全局作用域中被执行的,所以 this 指向 window 对象。要解决浏览器兼容性问题,可以使用 call 或 apply 方法来实现。
1 2 3 | setTimeOut ( function () {
obj.func.call(obj);
}, 100);
|
【相关推荐:javascript学习教程】
以上就是javascript中this什么意思的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
最完整指南 javascript 的错误处理
javascript的map方法有什么用
javascript怎么设置cookie
vue的响应式原理是什么?
javascript怎么增加样式
javascript是由那几个部分组成
javascript声明变量的4种方法是什么
使用h5实现react拖拽排序组件的方法(附代码)
如何给js创建一个数组对象
javascript怎么实现页面的刷新
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » javascript中this什么意思