【发布时间】:2026-01-24 03:00:01
【问题描述】:
当我从回调中调用 es6 类的方法时,我不能再将 this 作为我的对象:
class store{
constructor(){
this.value = 10;
};
printValue(self){
console.log(this);
console.log(self);//the value that I want print
};
};
class undefinedClass{
constructor(store){
this.store = store;
};
storeQuery(){
let ff = this.store.printValue;
setTimeout(ff, 300, this.store);
};
};
let i = new store();
let b = new undefinedClass(i);
b.storeQuery();
当我调用 b.storeQuery() 时,我想要打印的值是第二个。 有没有更优雅的方式来做到这一点?
【问题讨论】:
-
您似乎没有将
self参数传递给printValue... 它应该等于什么?