1. 在箭头函数出现之前,每个新定义的函数都有其自己的this值(例如,构造函数的 this 指向了一个新的对象;严格模式下的函数的 this 值为 undefined;如果函数是作为对象的方法被调用的,则其 this 指向了那个调用它的对象)。

2. 箭头函数没有自己的this,不会新产生自己作用域下的this,箭头函数里面的this指向它外层的环境里的this,它没有自己的this。arguments,super和new.target等对象。此外,箭头函数总是匿名的。

<input type="button" class="btn1" value="提交1">
<input type="button" class="btn2" value="提交2">

<script>
    $(".btn1").click(function () {
        console.log(1);
        console.log(this);// <input type="button" class="btn1" value="提交1">
    })

    $(".btn2").click(() => {
        console.log(2);
        console.log(this);// window
    })

</script>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-19
  • 2021-04-20
  • 2021-06-01
  • 2021-11-20
  • 2021-04-03
  • 2021-07-24
猜你喜欢
  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
  • 2021-10-24
  • 2021-11-21
  • 2021-06-11
相关资源
相似解决方案