0

I am learning ES6 classes and object. In order to that I'm making a sample app, and I found this issue:

I have this MainClass:

class App {
    boot() {
        console.log('app booted')
    }
}

Then I have another Class, that extends from the first one:

class someClass extends App {
    boot() {
        this.update()
    }
    update() {
        console.log('update Method!')
    }
}

From someClass I'm overwriting the boot method. This works fine, since it's trying to call update mothod.

But it's returning update method as undefined.

I get that, this in this case is the App class, so update is undefined inside App.

Is there a way to call the update method from the boot method in someClass class?

Pablo
  • 9,424
  • 17
  • 55
  • 78
  • 4
    *"I get that, this in this case is the App class, so update is undefined inside App."* No, that's incorrect. What `this` refers to depends on how you call `boot`. Maybe you are passing `boot` as a callback somewhere. In that case, have a look at [How to access the correct `this` / context inside a callback?](http://stackoverflow.com/q/20279484/218196). If that's not the case, please show us how you call it. – Felix Kling Apr 24 '16 at 05:19
  • 1
    I second Felix. Please add how you call, what return values you expect and what logging you expect and what you actually get in both terms. – try-catch-finally Apr 24 '16 at 07:46

1 Answers1

1

Yes, there is. Instantiate a new object and then call boot() on that object. For example like so:

class App {
    boot() {
        console.log('app booted')
    }
}

class someClass extends App {
    boot() {
        this.update()
    }
    update() {
        console.log('update Method!')
    }
}

const test = new someClass();
test.boot(); // update Method!

You can test your code here.

tomsp
  • 1,797
  • 14
  • 17