首页 > 其他 > 详细

es6 箭头函数 this 问题

时间:2018-02-09 18:36:36      阅读:201      评论:0      收藏:0      [点我收藏+]

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

2. 箭头函数没有自己的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>

 

es6 箭头函数 this 问题

原文:https://www.cnblogs.com/zhangruiqi/p/8436373.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!