31

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.

WeeniehuahuaXD
  • 852
  • 3
  • 10
  • 30
  • Mainly I'm trying to pass some objects to a different method in the class to process some work on it. – WeeniehuahuaXD Mar 04 '16 at 00:34
  • One issue I keep seeing, is that I can not call a method if I am inside of a separate function in the other method – WeeniehuahuaXD Mar 04 '16 at 00:34
  • 2
    Like always: `this.meth1()`. *"I can not call a method if I am inside of a separate function in the other method"* Sounds like a duplicate of [How to access the correct `this` / context inside a callback?](http://stackoverflow.com/q/20279484/218196) then. – Felix Kling Mar 04 '16 at 00:38
  • 2
    I can't read this without thinking, "don't do meth" – watzon Oct 17 '17 at 09:56

1 Answers1

36

Use this

import blah from './blaha';

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

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

  meth2(paramB) {
     this.meth1()
  }
}
dcohenb
  • 2,099
  • 1
  • 17
  • 34
  • Have a similar issue, with a subclass. Can't call a neighboring method within a subclass unless it is declared in a parent class. – kiwicomb123 Aug 15 '18 at 06:59
  • @kiwicomb123 that's how inheritance works. You should declare methods in parent class and use it in siblings. – Akansh Aug 07 '19 at 20:22
  • I never mentioned anything about inheritance. Not all methods of a subclass are relevant to their siblings. – kiwicomb123 Aug 07 '19 at 20:29