I have a question about how the keyword 'this' works in the following context. Here´s a tutorial with the phaser framework and the code looks like the following(I simply merged it together for you):
var game = new Phaser.Game(400, 490);
game.state.add('main', mainState);
game.state.start('main');
var mainState = {
preload: function() {},
create: function() {
this.bird = game.add.sprite(100, 245, 'bird');
},
update: function() {}
};
In the create function there is a 'this'. I think I understand what this does, but this example proved me wrong. The this
keyword - in this context- points to mainState
, right(just some information: the create function starts as soon as mainState
is called to start the 3rd line)?
I can access the bird outside the mainState
object (via mainstate.bird), but why isn't it possible then to define a prototype function like the following outside the game object?
mainState.prototype.myFunction() {}
I´ll get an error calling this and I can't explain.