1

How can I access object properties from functions which will be called as callback function. Please see code below. How can I access config property from processData function which will called when data is received from the server in ajax call.

MyClass: {
    config: {
        name: "",
        id: ""
    },

    loadData: function() {
        MyApi.getData(
            this.config,
            this.processData,   //sucess
            this.failureHandler //failure
        );
    },

    processData: function() {
        // how to access config object here? 
    }
}

Probably you can create an anonymous handler function and use call or apply to pass the this scope to actual handler function but is there a better solution than that?

4
  • possible duplicate of Preserve 'this' reference in javascript prototype event handler Commented Mar 31, 2015 at 20:55
  • this.processData.bind(this) is you don't want anonymous function. Commented Mar 31, 2015 at 21:12
  • Where is MyApi declared? Commented Apr 1, 2015 at 1:20
  • MyApi is another object written in a separate file: MyApp. MyApi with namespace. Think of it as a wrapper around $.ajax Commented Apr 1, 2015 at 17:38

0

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.