DDave
DDave

Reputation: 618

this is undefined in TypeScript

I have a class.

export class DatabaseHelper {

    public browserID : number;
    private ConfigID = 17;
}

Now, in the same class I am trying to access the ConfigID

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

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

this is giving me error as:

TypeError: Cannot set property 'browserID' of undefined

Upvotes: 1

Views: 1357

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123861

This is really the basic of a Typescript... use arrow function:

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

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

Read more about it

Arrow Function by Basarat

Upvotes: 3

Related Questions