![](https://i-blog.csdnimg.cn/blog_migrate/1c1eb883c392628a99e0bd8ddbcc1d77.png)
- 把事件处理函数,赋值给点击事件,类似于把类中的方法,复制给变量
- 这样一来, 原来的方法是由对象调用,this指向对象,而现在赋值给变量是直接调用,直接调用的话,this会指向window
- 但是,类中方法是局部作用域,都是开启了严格模式,因此this指向是undefined
代码释义上述内容:
class Person {
constructor(name){
this.name = name
}
handler(){
console.log(this);
}
}
const instance = new Person('Tom')
instance.handler() // 通过实例调用,打印实例
let temp = instance.handler
temp() // 直接调用,this指向window,但是由于类中的方法默认开启严格模式,所以this是undefined