1

For example I have the following class:

class X extends Y
{
   constructor() {  super(); } 

   method() {
      asyncMethod( function( err ) {   
          super.method( err );
      } );
    }
}

However, super is not the base class Y. How can I pass super to that callback?

Is there any solution than using arrow functions?

2 Answers 2

2

One possible solution is to use arrow functions, e.g.,

asyncMethod( err => super.method( err ) );
Sign up to request clarification or add additional context in comments.

1 Comment

Note that depending on the implementation of asyncMethod this code can behave differently (since it returns).
2

I would definitely go with arrow functions but, if you need an alternative, here it goes.

According to JavaScript closures behaviour, you can store a reference to super.method in a variable and use it within the callback.

Here's the code:

class X extends Y
{
   constructor() {  super(); } 

   method() {
      let superMethod = super.method;

      asyncMethod(function (err) {   
          superMethod(err);
      });
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.