学习笔记:箭头函数和普通函数的区别

394 阅读1分钟

1.箭头函数是匿名函数,不能用作构造函数,不能使用new。而普通函数可以用于构造函数,并创建对象。

2.箭头函数没有arguments,argument是JavaScript中的一个关键字,用于指向调用者传入的所有参数。在箭头函数中如果要拿到传入的所有参数,需要用到rest参数(...变量名)

const a = (...b) =>{
  console.log(b)
}
a(1,2,3) // [1,2,3]

3.this的作用域不同,在普通函数中,this是指向调用它的对象。如果用作构造函数,this指向创建的对象实例。而箭头函数本身没有this值,它所使用的this来自于函数作用域链。

const a = {
    b:10,
    c:() => {
        console.log(this.b)
    },
    d:function(){
        console.log(this.b)
    }
}
a.c() // undefined
a.d() // 10

4.箭头函数没有原型属性

const a = () => {}
console.log(a.prototype);  // undefined

5.箭头函数不能当做Generator函数,不能使用yield关键字