JS的this指向 笔记

this指向 一般指向调用它的对象

1. 全局作用域或者普通函数中,this指向window,(定时器中也是指向window)

function haha() {
   console.log(this)  //window
}
haha();

2. 谁调用this,指向谁

let shuaige = {
      name: function(){
               console.log(this)   //shuaigge
  }
}
shuaige.name();

 3. 函数中this指向构造函数的实例

function box (){

     console.log(this)    

}
box();   // window
  let Box = new box();  //function box();

欢迎讨论

原文地址:https://www.cnblogs.com/yanghai/p/11535537.html