箭头函数的语法:
const a = x => x+1;
const b = (x, y) => x+y;
const c = (x, y) => {
console.log(x, y);
return x + y;
}
箭头函数与普通函数的区别:
1、不绑定this
箭头函数则会捕获其所在上下文的 this
值,作为自己的 this
值
2、箭头函数不绑定arguments,但是可使用…rest参数
3、箭头函数的call()或apply()函数,不会影响到this绑定的对象
4、箭头函数没有原型属性(prototype)
箭头函数不适用的场景
1、构造函数的原型方法上
Person.prototype.getFirst = function(){}
2、需要获取 arguments
箭头函数中不具备 arguments 伪数组
3、使用对象的方法
const Person = {
getName:function(){}
}
4、使用动态回调时
例如 addEventListener 绑定的方法,如果使用箭头函数 this就会指向window
参考: