箭头函数是匿名函数,不绑定自己的this,arguments,super,new.target
- 箭头函数会捕获其所在上下文的
this值,作为自己的this值,在使用call/apply绑定时,相当于只是传入了参数,对this没有影响 
let myObject = {
    value:1,
    add:function(a){
        let f = (a) => a + this.value;
        return f(a);
    },
    addCall:function(a){
        let f = (a) => a + this.value;
        let b = {value:2};
        return f.call(b,a);
    }
};
console.log(myObject.add(1));        //2
console.log(myObject.addCall(1));    //2
- 箭头函数不绑定
arguments,取而代之用rest参数…解决 
const fun = (...args)=>{
    console.log(args) //数组形式输出参数
};
fun(1,2,3);  // [ 1, 2, 3 ]
//普通函数可以使用arguments
function log() {
    console.log.apply(console,arguments);
}
log(1,2,3);  //1 2 3
- 箭头函数当方法使用的时候,没有定义
this绑定 
const obj = {
    i: 10,
    b: () => console.log(this.i),
    c: function() {
        console.log( this.i)
    }
}
obj.b(); //undefined
obj.c(); //10
- 箭头函数不能作为构造函数,和 new 一起用就会抛出错误
 
const Foo = () => {};
new Foo(); 
// TypeError: Foo is not a constructor
- 箭头函数没有原型属性
 
const Foo = () => {};
console.log(Foo.prototype); 
// undefined
- 不能简单返回对象字面量
 
let func = () => { foo: 1 };
console.log(func());  //undefined
//如果要返回对象字面量,用括号包裹字面量
let func = () => ({ foo: 1 });
console.log(func());   //{ foo: 1 }
- 箭头函数不能当做Generator函数,不能使用yield关键字
 - 箭头函数不能换行
 
参考文章推荐
箭头函数
箭头函数与普通函数的区别










网友评论