naughty boy naughty boy - 7 months ago 18
Javascript Question

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.

Answer

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));
Comments