I’m reusing an old application (a game) so it’s possible to run several games at ones.
By that reason I’ve changed the properties to “this.propery”, which are used everywhere in my application.
However, the only prototype function that can access the properties is “startGame”.
I have tried both “this.bricks” and “Game.bricks”, but both are undefined when trying to reach them in any other function that “startGame”.
Any tips for me?
var game = new Game();
game.startGame();
Game = function(){
this.bricks = 2;
this.checkOddEven = 0;
...
}
Game.prototype.startGame = function() {
console.log(this.bricks) <- 2
console.log(Game.bricks) <- 2
// Code goes here...
Game.prototype.renderTiles()
}
Game.prototype.renderTiles = function() {
// code goes here...
console.log(this.bricks) <- undefined
console.log(Game.bricks) <- undefined
}
… the same goes for the other prototype functions.
You are calling
renderTilesin the wrong way.thiswill refer toGame.prototypeinstead ofgame(theGameinstance).Call it with:
What
thisrefers to inside a function depends on how the function is called. MDN provides a good article [MDN] about that.FWIW:
As long as you are not assigning properties directly to the
Gamefunction,Game.bricksshould beundefinedas well, not matter where you access it and how you call the function.