JS普通函数和箭头函数的区别:
(1)箭头函数的this是静态的,this始终会指向函数声明时所在的作用域下,用call()修改this的指向,是无效的。
function getName1(){
console.log(this.name);
}
let getName2=()=>{
console.log(this.name);
}
window.name='黄美丽';
const school={
name:"scnu"
}
getName1.call(school);
getName2.call(school);
(2)箭头函数不能做为构造函数实例化对象。
let test=(name,age)=>{
this.name=name;
this.age=age;
}
let te=new test('黄美丽','18');
(3)箭头函数不能使用arguments变量,arguments变量的使用。
let test=()=>{
console.log(arguments);
}
test('黄美丽','18');
(4)箭头函数可以简写。
//当函数有且只有一个参数时,可以省略小括号
let test1=te1=>{
return true;
}
/*当代码体只有一条语句的时候,可以省略花括号,若该语句是返回语句时,可以省略return,
语句的执行结果就是函数的返回值。*/
let test2=te2=>true;