1

I have this code:

class ExamPage {

  tabClear() {
    page.clear(this.examName);
    page.clear(this.examVersionId);
  }

  createNew() {
    describe('Create new' , function() {
      it('Clear input boxes', function() {
        tabClear(); // <<< not recognized 
      });
    });
  }

}

Can someone advise me. I would like to call the function tabClear() but I cannot access it. Can someone tell me how I can do this

1 Answer 1

3

In case we need to call function of our own class we always have to use this

class ExamPage {

  tabClear() {
    page.clear(this.examName);
    page.clear(this.examVersionId);
  }

  createNew() {
    describe('Create new' , function() { 
      it('Clear input boxes', function() {
        this.tabClear(); // HERE the this.tabClear() is the answer
      });
    });
  }    
}

But in fact, we also should use arrow function notation, which will keep the correct scope of the this:

createNew() {
    // the function () is replaced with arrow function
    describe('Create new' , () => {
      it('Clear input boxes', () => {
        this.tabClear(); // HERE the this.tabClear() is the answer
      });
    });
  } 

see more details about arrow functions here:

small cite:

“Arrow function expressions are a compact form of function expressions that omit the function keyword and have lexical scoping of this.” Basically the Arrow Function helps you retain a certain scope automatically. If you look at the outputted code from the compiler, it just creates a var _this = this; and it is used inside the function.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.