Trigger One Prototype Function From Another (including Itself)
The Problem I have created a constructor called Game. Then, I tried to expand it's functionality by adding two new functions (update and render) using prototype. However, I'd like
Solution 1:
Those lines
requestAnimationFrame(this.render);
requestAnimationFrame(this.update);
should be
requestAnimationFrame(this.render.bind(this));
requestAnimationFrame(this.update.bind(this));
Otherwise, in the second execution of your recursion, keyword this
will refer to window
object instead of Game
. And window.update
is obviously not a function, as the error specified.
Solution 2:
You have lost the reference to the Game object, try binding 'this' like so:
varGame = function(){
};
Game.prototype.update = function(){
requestAnimationFrame(this.render.bind(this));
requestAnimationFrame(this.update.bind(this));
};
Game.prototype.render = function(){
console.log('Rendering');
};
var game = newGame();
game.update();
Post a Comment for "Trigger One Prototype Function From Another (including Itself)"