1.函数中:
function foo(){
return this;
}函数调用者是谁,就指向谁;直接调用指向window;
2.事件中:html事件中,指向window;dom0事件中,指向事件的触发者(绑定元素的节点);dom2事件中,非IE中指向绑定的元素节点;IE中直接指向window;
3.闭包中:this指向window;
4.对象中:this指向当前对象;如果有多级对象包裹,指代上一级对象;
( 1).
var foo = {
a:18,
num:{
a:10,
num:function(){
console.log(this.a);//10
}
}
}
foo.num.num();(2).
var foo = {
a:18,
num:{
num:function(){
console.log(this.a);//undefined
}
}
}
foo.num.num();5.call函数和apply函数能改变this的指向,bind函数也能改变函数指向;
6.构造函数模块:
总结:构造函数中,返回值是基本数据类型,那么this指向构造函数的实例;返回值是对象则this指向该对象;
function Foo(){
this.user = 'my';
return {};
}
var na = new Foo();
console.log(na.user);//返回值undefined;
function Foo(){
this.user = 'my';
return 1;
}
var na = new Foo();
console.log(na.user);//返回值my以上就是this对象的指向含义的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号