this指向

1.在全局使用this

  console.log(this)//this指向window

2.函数中的this

  function show(){
    consloe.log(this);//window,谁调用,this就指向谁
  }
show()

3.事件绑定中的this

  var box = document.querySelector(".box")
          box.onclick = function(){
                console.log(this);  //box, 谁绑定事件,this就是谁
          }
  1. 事件绑定中,调用其它函数的this
   var box = document.querySelector(".box")
             function show(){
                 console.log(this);  //window,谁调用,this是谁
             }
             box.onclick = function () {
                console.log(this); //this指向box
                 show();
             }

5.定时器中的this: 无论定时器在哪出现,其内部的this都指window

  setInterval(function(){
                     console.log(this) //window
                 },3000)
  1. 构造函数和原型中的this
  //this指向new出来的新的实例化对象

改变this指向

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。