Game.prototype.run = function() {
window.setInterval(function() {
var thisLoop = new Date().getTime();
this.update();
this.render();
lastLoop = thisLoop;
}, 1000 / this.fps);
};
game.js:198Uncaught TypeError: Object [object DOMWindow] has no method ‘update’
Why is this happening ?
“this” should relate to the Game object.
Cache the
thisvariable, or useFunction.bind:Or, using
Function.bind:thisin a function passed tosetIntervalrefers to the globalwindowobject, or isundefined(in strict mode).Another method, similar to the first one. Pass
thisas a parameter to the function (so that no extra local variable is used):