es6的箭头函数和es5的function函数区别

一.es6的箭头函数

  • es6箭头函数内部没有this,使用时会上朔寻找最近的this
  • 不可以做构造函数,不能使用new命令,因为没有this
  • 函数体内没有arguments,可以使用rest参数代替
  • 不能用yield,不能使用generator函数

二.疑问

下面代码中的箭头函数arrows的this指向window

    let obj = {
        aaa: '123',
        arrows : () => {
            console.log('arrows',this);
        },
        func : function () {
            console.log('func',this);
        }
    }

    obj.arrows();   // window
    obj.func();     // obj

猜你喜欢

转载自blog.csdn.net/guxiansheng1991/article/details/80009144
今日推荐