箭头函数与普通函数的区别
箭头函数:
let fun = () => {
console.log('a');
}
普通函数:
function fun() {
console.log('a');
}
箭头函数是匿名函数,不能作为构造函数,不能使用new
let FunConstructor = () => {
console.log('lll');
}
let fc = new FunConstructor();//使用new会报错
总结
箭头函数的 this 永远指向其上下文的 this ,任何方法都改变不了其指向,如 call() , bind() , apply()
普通函数的this指向调用它的那个对象
箭头函数与普通函数
1、箭头函数没有自己的this,继承外层代码块的this。
2、不能当做构造函数,也就是说不可以使用new命令,否则会报错的。
3、不能使用arguments对象,该对象在函数体内不存在。如果要用,可以用 rest 参数代替。
4、不能使用yield命令,因此箭头函数不能用作 Generator(生成器) 函数。
5、因为没有this,所以不能使用call、bind、apply来改变this的指向。