const obj = {
x: 1,
print1: () => {
console.log(this.x)
},
print2() {
console.log(this.x)
},
print3: function() {
console.log(this.x)
}.bind(this)
}
obj.print1() // 执行结果 undefined this指向 window
obj.print2() // 执行结果 1 this指向 obj
obj.print3() // 执行结果 undefined this指向 window
总结:普通函数,谁调用 this就指向谁,代码中是obj调用, 指向obj。箭头函数是看上一层,obj调用,上一层就是window,箭头函数没有 this。如果访问 this,则会从外部获取。