i know that i have to keep a reference to this inside a JS class, when i need to access class member functions. However, i am currently struggling with the following (simplified) piece of code:
function MySimpleClass(p, arr) {
this.proxy = p;
this.contentArray = arr;
this.doStuff = function(callback) {
var self = this;
// at this point this.contentArray holds data
this.proxy.calculate(function(data) {
// inside the anonymous function this.contentArray is undefined
var el = self.contentArray[0]; // <-- will fail
// do something with el
callback.call(this, data);
});
}}
Any help is appreciated!
This sample code with your class is working:
I assume that your code is not working because you defined also a “self” variable in the proxy class itself. You can check that by renaming “self” in your class to something arbitrary: “selfXXX”