1

Let's say I have a class:

var asdf = new Class({
  myFunction: function () {
    //some stuff here
  },
  anotherFunction: function() {
    globalObject.dosomethingandusecallback( 
      function() { // this is the callback
        //how do I call myFunction() here? I can't seem to get it to work?
      }
    );
  }
});

I seem to have some scoping problems in trying to call myFunction within the definition of my callback function. What am I missing here? I thought it should have access to myFunction in this context?

Thanks!

1 Answer 1

3

Copy the this keyword into a variable outside of the callback function, and use that variable inside the callback:

anotherFunction: function() {
  var self = this;
  globalObject.dosomethingandusecallback( 
    function() { // this is the callback
      self.myFunction();
    }
  );
}
Sign up to request clarification or add additional context in comments.

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.