箭头函数
let fn = ()=> {}
如果只有一个参数,可以省()
let fn = e => {}
如果只有一条表达式,可以省略{}和return
let fn = e => e*e;
普通函数
function fn(){}
1、箭头函数是匿名函数,不能作为构造函数,不能使用new。
let Fn = ()=> {}
fn = new Fn();
2、箭头函数不绑定实参 anguments ,rest 参数(...)解决
let fn = (...arg)=>{
console.log(arg);
}
function fn(arg){
console.log(arguments);
}
fn(2,3,4);
3、箭头函数不绑定this,上下文作为其this值
obj={
name:'wq',
fn:()=>{
console.log(this);
},
fn2:function(){
console.log(this);
}
}
4、箭头函数通过 call() 和 apply() 调用一个函数时 , 只传一个参数时,对 this 没有影响。
5、箭头函数没有原型属性
let fn = ()=>{}
function fn(){}
fn.prototype
6、箭头函数不能当generator
function *fn(){
yild
}
总结:箭头函数 this 指向上下文,无法改变
普通函数this 指向当前其调用对象