naughty boy
naughty boy

Reputation: 2149

Calling function on parent element give 'super' keyword unexpected here

I have a typical call hierarchy

class A
{
   mirm(){
     //stuff here
   }
}

class B extends A
{
   constructor(){
     //obtain a promise
     promise().then(this.setUp)
   }

   setUp(){
    super.mirm();
   }
}

Could it be that the promise might be doing something to the scope? I would actually expect for you to be able to do something like this.mirm() from the setUp function since it should just follow the prototype chain. What gives? I am compiling with babel and have es2015 as target.

Upvotes: 0

Views: 350

Answers (1)

Paul S.
Paul S.

Reputation: 66324

Passing this.setUp detaches the current context object from the function reference when it gets invoked, therefore the this in the super's method is confused about where to look

Consider wrapping it instead

// arrow function preserves context
foo.then(() => this.setUp());
// OR
// binding it to explicitly give context
foo.then(this.setUp.bind(this));

Upvotes: 3

Related Questions