- 箭头函数使用箭头定义,普通函数中没有
- 箭头函数都是匿名函数,而普通函数可以是匿名函数,也可以是具体名函数;
// 具名函数
function func(){
// code
}
// 匿名函数
let func=function(){
// code
}
// 箭头函数全都是匿名函数
let func=()=>{
// code
- 普通函数可以用于构造函数,以此创建对象实例;箭头函数不能用于构造函数,不能使用new;
function Person(name,age){
this.name=name;
this.age=age;
}
let admin=new Person("恩诺小弦",18);
console.log(admin.name);
console.log(admin.age);
- 箭头函数的 this 永远指向其上下文的 this,而普通函数的this指向调用它的那个对象
- 箭头函数不绑定arguments,取而代之用rest参数…解决
每一个普通函数调用后都具有一个arguments对象,用来存储实际传递的参数。但是箭头函数并没有此对象。
function A(a){
console.log(arguments);
}
A(1,2,3,4,5,8); // [1, 2, 3, 4, 5, 8, callee: ƒ, Symbol(Symbol.iterator): ƒ]
let B = (b)=>{
console.log(arguments);
}
B(2,92,32,32); // Uncaught ReferenceError: arguments is not defined
let C = (...c) => {
console.log(c);
}
C(3,82,32,11323); // [3, 82, 32, 11323]
详细参见:原文链接:https://blog.csdn.net/m0_64346035/article/details/124668021