Open Samgao0312 opened 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
this
function
window
总结
this
跟外层function
的 this 一致,外层function
的this
指向谁,箭头函数的this
就指向谁。如果外层不是function
则指向window
;参阅