1. 定义
-
函数
function getName() {}
-
箭头函数
const getName = () => {}
2. 命名
-
函数分为匿名、具名。
function getName() {} let getName = function () {}
-
箭头函数只有匿名。
const getName = () => {}
3. 构造函数
箭头函数都是匿名函数,所以不能作为构造函数。
使用箭头函数没有自己的词法上下文,因此不会有作用域 this
,也不能用于构造函数。
let gen = () => {}
new gen()
new gen() // VM82:4 Uncaught TypeError: gen is not a constructor at :4:10
^TypeError: gen is not a constructor
4. arguments
略
5. 原型对象
-
函数
function getName() {} console.log(getName.prototype)
{}
-
箭头函数没有原型对象。
const getName = () => {} console.log(getName.prototype)
undefined
6. 简写方式
箭头函数返回一个对象时,可以使用 ()
包裹,并省略 return
。
const getKV = () => {
return {age: 12}
}
简化后
const getKV = () => ({age: 12})