this对象
灵感胜于汗水 Lv5

定义

函数的 this 关键字在 JavaScript 中的表现略有不同,此外,在严格模式和非严格模式之间也会有一些差别。

在绝大多数情况下,函数的调用方式决定了 this 的值(运行时绑定)。

this 关键字是函数运行时自动生成的一个内部对象,只能在函数内部使用,总指向调用它的对象,指代函数当前的运行环境。

同时,this在函数执行过程中,this一旦被确定了,就不可以再更改。

默认绑定

全局环境中定义person函数,内部使用this关键字

1
2
3
4
5
var name = 'Jenny';
function person() {
return this.name;
}
console.log(person()); //Jenny

上述代码输出Jenny,原因是调用函数的对象在游览器中位window,因此this指向window,所以输出Jenny

注意:严格模式下,不能将全局对象用于默认绑定,this会绑定到undefined,只有函数运行在非严格模式下,默认绑定才能绑定到全局对象。

隐式绑定

函数还可以作为某个对象的方法调用,这时this就指这个上级对象

1
2
3
4
5
6
7
8
9
function test() {
console.log(this.x);
}

var obj = {};
obj.x = 1;
obj.m = test;

obj.m(); // 1

这个函数中包含多个对象,尽管这个函数是被最外层的对象所调用,this指向的也只是它上一级的对象

1
2
3
4
5
6
7
8
9
var o = {
a:10,
b:{
fn:function(){
console.log(this.a); //undefined
}
}
}
o.b.fn();

上述代码中,this的上一级对象为bb内部并没有a变量的定义,所以输出undefined

1
2
3
4
5
6
7
8
9
10
11
12
var o = {
a:10,
b:{
a:12,
fn:function(){
console.log(this.a); //undefined
console.log(this); //window
}
}
}
var j = o.b.fn;
j();

此时this指向的是window,这里的大家需要记住,this永远指向的是最后调用它的对象,虽然fn是对象b的方法,但是fn赋值给j时候并没有执行,所以最终指向window

new绑定

通过构建函数new关键字生成一个实例对象,此时this指向这个实例对象

1
2
3
4
5
6
function test() {
 this.x = 1;
}

var obj = new test();
obj.x // 1

上述代码之所以能过输出1,是因为new关键字改变了this的指向

new过程遇到return一个对象,此时this指向为返回的对象

1
2
3
4
5
6
function fn() {  
this.user = 'xxx';
return {};
}
var a = new fn();
console.log(a.user); //undefined

如果返回一个简单类型的时候,则this指向实例对象

1
2
3
4
5
6
function fn() {  
this.user = 'xxx';
return 1;
}
var a = new fn;
console.log(a.user); //xxx

注意的是null虽然也是对象,但是此时new仍然指向实例对象

1
2
3
4
5
6
function fn() {  
this.user = 'xxx';
return null;
}
var a = new fn;
console.log(a.user); //xxx

显示修改

apply()、call()、bind()是函数的一个方法,作用是改变函数的调用对象。它的第一个参数就表示改变后的调用这个函数的对象。因此,这时this指的就是这第一个参数

1
2
3
4
5
6
7
8
9
var x = 0;
function test() {
 console.log(this.x);
}

var obj = {};
obj.x = 1;
obj.m = test;
obj.m.apply(obj) // 1

箭头函数

ES6 的语法中还提供了箭头函语法,让我们在代码书写时就能确定 this 的指向(编译时绑定)

1
2
3
4
5
6
7
8
9
const obj = {
sayThis: () => {
console.log(this);
}
};

obj.sayThis(); // window 因为 JavaScript 没有块作用域,所以在定义 sayThis 的时候,里面的 this 就绑到 window 上去了
const globalSay = obj.sayThis;
globalSay(); // window

虽然箭头函数的this能够在编译的时候就确定了this的指向,但也需要注意一些潜在的坑。

  • 绑定事件监听
1
2
3
4
5
const button = document.getElementById('mngb');
button.addEventListener('click', ()=> {
console.log(this === window) // true
this.innerHTML = 'clicked button'
})

上述可以看到,我们其实是想要this为点击的button,但此时this指向了window

  • 在原型上添加方法时候,此时this指向window
1
2
3
4
5
6
Cat.prototype.sayName = () => {
console.log(this === window) //true
return this.name
}
const cat = new Cat('mm');
cat.sayName()

同样的,箭头函数不能作为构建函数

事件中的this

在HTML事件句柄中,this指向了接收事件的HTML元素

1
2
3
4
let oBtn = document.querySelector('#btn');
oBtn.addEventListener('click',function(){
console.log(this); //oBtn
})

定时器会改变this指向

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
oBtn.addEventListener('click',function(){
setTimeout(function(){
console.log(this); //Window
},1000)
})

//显示修改this
oBtn.addEventListener('click',function(){
setTimeout(function(){
console.log(this); //oBtn
}.bind(this),1000)
})

//使用箭头函数
oBtn.addEventListener('click',function(){
setTimeout(()=>{
console.log(this); //oBtn
},1000)
})

优先级

new绑定优先级 > 显示绑定优先级 > 隐式绑定优先级 > 默认绑定优先级

  • 本文标题:this对象
  • 本文作者:灵感胜于汗水
  • 创建时间:2022-10-29 18:19:20
  • 本文链接:https://cjhsyc.github.io/2022/10/29/this对象/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!