Jump to content

What does keyword 'this' refer to in code?


ldurniat
 Share

Recommended Posts

Hi,

 

I' working on simple game in XDK Intel and Phaser lib. I use fresh template from XDK and I can not understand why this code work. A specially What does keyword 'this' refer to in code(init function) below? I have read about closure function but it doesnt help me:(

 

Game.js:

BasicGame = {};BasicGame.Game = function (game) {};BasicGame.Game.prototype = {    init: function () {        this.input.maxPointers = 1;       this.stage.disableVisibilityChange = true;        this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;       this.scale.pageAlignHorizontally = true;        this.scale.pageAlignVertically = true;        this.scale.forceOrientation(false, true);        this.scale.setResizeCallback(this.gameResized, this);       this.scale.updateLayout(true);       this.scale.refresh();    },    preload: function () {        this.load.image('logo', 'asset/phaser.png');    },    create: function () {        this.logo = this.add.sprite(            this.world.centerX,             this.world.centerY,            'logo');        this.logo.anchor.setTo(0.5, 0.5);    },    gameResized: function (width, height) {}};

app.js

(function () {    var game = new Phaser.Game(480, 640, Phaser.AUTO, 'game');    game.state.add('Game', BasicGame.Game);    game.state.start('Game');})();

I have found in doc of Phaser.StateManager:

 

The State can be either a Phaser.State object (or an object that extends it), a plain JavaScript object or a function. If a function is given a new state object will be created by calling it.

 

BasicGame.Game is a function but why keyword 'this' from inside may refer to game object. I dont know.

Edited by ldurniat
Link to comment
Share on other sites

Those are prototype methods for the Game class, within those functions the 'this' keyword will refer to the Game instance the method has been called for.

I'm not sure what you try to say me. Can you tell me that in simpler way?

Do you mean object for what is invoked init method? 

I have found this code in phaser/StateManager.js

else if (typeof state === 'function')        {            newState = new state(this.game);        }

In my example state object is BasicGame.Game. Is this correct?

Link to comment
Share on other sites

Re your edit:

BasicGame.Game is a function but why keyword 'this' from inside may refer to game object. I dont know.

It's a class / constructor

 

I'm not sure what you try to say me. Can you tell me that in simpler way?

No, I don't believe that is something I can do.

 

I have found this code in phaser/StateManager.js

else if (typeof state === 'function')        {            newState = new state(this.game);        }
In my example state object is BasicGame.Game. Is this correct?
You have a "module" BasicGame which has a "class" Game and that has prototype "methods": init, preload, create, and gameResized.

Quotes in the preceding sentence because Javascript as a language uses "prototypal inheritence" model of OO (rather than Class-based OO) so it doesn't strictly enforce those patterns the way Java or c++ might

Link to comment
Share on other sites

Usually, "this" refers to "this object." For example: within Game, "this" refers to this instance of the Game object; within StateManager, "this" refers to this instance of the StateManager object. It's extremely useful! You will use "this" often and it will make your life much easier. Just when you think you understand it perfectly, however, you'll find that there's more to learn about scoping properly. ;)

Link to comment
Share on other sites

/**    * Links game properties to the State given by the key.    *    * @method Phaser.StateManager#link    * @param {string} key - State key.    * @protected    */    link: function (key) { --->       this.states[key].game = this.game;

This what I was looking for:) Last line. Now I know why code(see my first post) in template work. Thanks everbody for help. It is a time for a next step in creating my first cool game:):):)  

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...