es6 call class methods from within same class

2019-03-23 10:50发布

问题:

I'm trying to call a class method in my class form a neighboring method as shown in the example below.

import blah from './blaha';

export default class myclass{
  constructor(con) {
    this.config = con;
  }

  async meth1(paramA) {
    //do_stuff...
  }

    meth2(paramB) {
     //attempt to call meth1()
  }

}

I would like to call a method from within a different method using es6 class styles.

回答1:

Use this

import blah from './blaha';

export default class myclass{
  constructor(con) {
    this.config = con;
  }

  async meth1(paramA) {
    //do_stuff...
  }

  meth2(paramB) {
     this.meth1()
  }
}