由于箭头函数的this在定义时确定, 所以有一些地方不能用箭头函数
- 不能定义对象上的方法
let obj = {
a: 1,
func: () => {
console.log(this.a)
}
}
var a = 0
obj.func() // 0
- 不能定义动态上下文的回调函数
const btn = document.getElementById('id')
btn.addEventListener('click', () => {
console.log(this === window) // => true
})
- 不能作为构造函数
const Mes = (a) => {
this.a = a
}
const kk = new Mes('hh');
// Throws "TypeError: Message is not a constructor"