Given class functions
game.PlayScreen = me.ScreenObject.extend({
onResetEvent: function() {
this.setAll(); //calls setAll(), which calls setGlobals()
this.saveNextLevelData(this.setAll);
},
saveNextLevelData : function (callback) {
$.get("./php/CRUD.php", {},
function (returned_data) {
callback(); //however, the callback of setAll throws
`undefined is not a function` error
}
},
setAll : function () {
log("setAll called");
this.setGlobals();
},
setGlobals : function () {
log("setGlobals called");
}
});
Basically, I'm confused on how this
context is lost when you call a callback function.
In the above code,
Works:
this.setAll()
called directly fromonResetEvent
outputs"setAll called"
and"setGlobals called"
Breaks:
callback()
callingthis.setAll()
from$.get
outputs"setAll called"
butthis.setGlobals();
breaks... I think due to lostthis
context... It outputsUncaught TypeError: undefined is not a function
I'm trying to follow the context of this
when you call a callback function which contains a function belonging to the parent object (this
, in this case). If I want to call this.setGlobals()
from a callback of this.setAll()
, where exactly do I need to do the bind?
Thanks