【发布时间】:2020-09-18 06:39:18
【问题描述】:
class b {
constructor(){
this.name = 'bar'
}
b1(){
console.log('here: ', this.name);
function c() {
console.log('inside c: ', this.name)
}
c();
}
}
let a = new b;
a.b1();
//output:
// here: bar
// inside c: undefined
在这种情况下,当调用a.b1()时,在函数b1的范围内,this上下文被绑定到a。但是在函数b1内部执行函数c时,为什么this上下文丢失了? this 假设在函数 c 的闭包中?
我知道如何让它工作(箭头功能)。只是想知道为什么。
【问题讨论】:
标签: javascript class closures this