可以在then中设置this.variable吗?我知道我可以传递一个常规变量,但是在构造函数中设置一个变量就可以做到这一点吗?
class Test {
constructor() {
this.variable = false;
}
aFunction() {
someExternalThing(document.body, {
option: 1
}).then(function () {
this.variable = true;
});
}
}发布于 2020-12-31 19:36:29
this关键字引用的是函数。试试这个:
aFunction() {
const that = this;
someExternalThing(document.body, {
option: 1
}).then(function () {
that.variable = true;
});
}https://stackoverflow.com/questions/65520063
复制相似问题