Samgao0312 / Blog

MIT License
1 stars 1 forks source link

this 指向问题梳理~ #70

Open Samgao0312 opened 4 years ago

Samgao0312 commented 4 years ago
let obj = {
    num: 1,
    counter: () => this.num++
}
obj.counter(); // NaN
console.log(obj.num)  // 1
function foo() {
  setTimeout(() => {
    console.log('id: ', this.id)
  }, 100)
}
let id = 21

foo() // id: 21
foo.call({ id: 42 }) // id: 42

总结

参阅