【问题标题】:this is undefined in TypeScript这是在 TypeScript 中未定义的
【发布时间】:2016-06-07 06:26:44
【问题描述】:

我有课。

export class DatabaseHelper {

    public browserID : number;
    private ConfigID = 17;
}

现在,在同一个班级中,我尝试访问 ConfigID

SetBrowserID() {
        browser.getCapabilities().then(function (cap) {

            this.browserID = new commFnctions.appdata().getBrowserIDFromBrowserName(cap.get('browserName'));
        });
    }

这给了我错误:

TypeError: Cannot set property 'browserID' of undefined

【问题讨论】:

标签: typescript protractor angularjs-e2e e2e-testing


【解决方案1】:

这确实是 Typescript 的基础...使用 箭头函数

SetBrowserID() {
    browser.getCapabilities()
       //.then(function (cap) {
       .then((cap) => {

        this.browserID = new commFnctions
                .appdata()
                .getBrowserIDFromBrowserName(cap.get('browserName'));
    });
}

了解更多信息

Arrow Function by Basarat

【讨论】:

  • 如果这有帮助的话.. 太棒了 ;) 享受 Typescript
猜你喜欢
  • 2021-11-04
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 2019-12-11
相关资源
最近更新 更多