我有以下结构 Javacript es5-es6 并且控制器类在 Get 类中丢失了引用,我已经在调查但我找不到如何避免丢失引用。
class Controller {
constructor() {
this.name = 'Test';
}
test() {
console.log(1, this.name);
}
}
referenceController = new Controller();
// working reference: console.log(1, 'Test');
referenceController.test();
class Get {
method() {
return {
controller: referenceController.test
}
}
}
// Lost self reference: console.log(1, undefined)
new Get().method().controller()