當我打電話從一個回調的ES6類的方法,我可以不再引用this
作爲我的對象:在ES6類這個/自
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()THA值我想要打印是第二個。 有沒有更好的方法來做到這一點?
你似乎並不被傳遞'self'參數爲'printValue' ..它應該等於什麼? – Tuvia
請參閱[如何在回調中訪問正確的'this' /上下文?](http://stackoverflow.com/q/20279484/218196) –