Tuesday, 15 January 2019

JavaScript this scope in callback functions

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?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...