微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ES6 箭头函数使用总结

前言

ES6中添加了箭头函数,可以更方便绑定this作用域了 O.o
至于使用,我觉得一个实例就够了

实例代码

const x = 1;
global.x = 2;
const obj = {
    x: 3,
    fun: function () {
        console.log(this.x); // 3 this => obj
        let fun2 =  () => {
            console.log(this.x); // 3 this => fun => obj
        };
        let fun3 = function () {
            console.log(this.x); // 2 this => global
        };
        fun2();
        fun3();
    }
};

obj.fun();
console.log(this.x); // undefined this => {}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐