Jump to content

prototype difference vs posx and this.posx


espace
 Share

Recommended Posts

hi,

i would understand the difference between example 1 and example 2 with the usage of "this.posx" and simply "posx"

is it the same ? why in some code i see the usage of this.something ?

//example 1
object_physics = function(game,posx,posy,im,Group){
this.posx=posx
this.posy=posy    
    Phaser.Sprite.call(this,game,this.posx,this.posy,im)
    game.physics.arcade.enable(this)
    this.body.velocity.y=10
    Group.add(this)
}

object_physics.prototype = Object.create(Phaser.Sprite.prototype)
object_physics.prototype.constructor = object_physics

//example 2
object_physics = function(game,posx,posy,im,Group){
    Phaser.Sprite.call(this,game,posx,posy,im)
    game.physics.arcade.enable(this)
    this.body.velocity.y=10
    Group.add(this)
}

object_physics.prototype = Object.create(Phaser.Sprite.prototype)
object_physics.prototype.constructor = object_physics

 

Link to comment
Share on other sites

Hey,

So both are doing the same as I can see.
If you use ES6 the syntax will be much clearer and I think more understandable.

what EX1 does is assigning new properties (this.posx) to the Object (object_physics). So that object_physics.posx will return a value.
this is a good practise if your class (Sprite or something) has certain methods. You can reference and access the properties inside.

I have written an example for better understanding:
Your Hero can collect items, there is a method for this and since we referenced the items by "this.data.itemsCollected", we can access "this.data.itemsCollected".
 

class Hero extends Phaser.Sprite {
  constructor( x, y ){
    //x and y are passed by the code at the bottom, by calling 'new'.
    super( x, y, 'Hero-image.png');
    //super calls the Phaser.Sprite and passes all 
    //parameters: x, y, 'Hero-image.png' into the constructor of the Phaser.Sprite class.

    this.data.name = 'Super Meat Boy';
    this.data.itemsCollected = 0;
    this.data.abilities = [];
    // so in the constructor we are setting up the Hero and assigning values on properties (this.something)
  }

  collectItem( item ) {
    //an item is collected and passed as a parameter into the method call.
    this.data.itemsCollected.push( item );
    //now that we assigned a property(this.something | this.data.itemsCollected)
    //to out Hero, we can use it inside his methods 
    //to do things like modifying that value the property keeps.
  }


  jump() {
    if( this.blocked.down) {
      this.body.velocity.y = -100;
      return true;
    }
    return false;
  }

  //..more code ..
}


new Hero( 20, 50);


Why not saving everything with global variables?
- because of performance
- clean code
- "this" relates to the certain objects, with a global var you won't have the reference
- reusability (classes can be extended = used by other "subheroes" and being extended, means: they all share the same methods

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...