首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在then的构造函数中使用此集合

在then的构造函数中使用此集合
EN

Stack Overflow用户
提问于 2020-12-31 19:07:55
回答 2查看 32关注 0票数 0

可以在then中设置this.variable吗?我知道我可以传递一个常规变量,但是在构造函数中设置一个变量就可以做到这一点吗?

代码语言:javascript
复制
class Test {
    constructor() {
        this.variable = false;
    }

    aFunction() {
        someExternalThing(document.body, {
            option: 1
        }).then(function () {
            this.variable = true;
        });
    }
}
EN

Stack Overflow用户

发布于 2020-12-31 19:36:29

this关键字引用的是函数。试试这个:

代码语言:javascript
复制
aFunction() {
  const that = this;
  someExternalThing(document.body, {
     option: 1
  }).then(function () {
     that.variable = true;
  });
}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65520063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档