Jump to content

function prototyping ?


phaserNOOB
 Share

Recommended Posts

I am learning PHASER HTML5 game dev framework based on javascript, 

during which I came across this piece  of code which I am not able to understand

 

 

 

    var BunnyDefender = {};        

 

    BunnyDefender.Boot = function(game) {};

    

    BunnyDefender.Boot.prototype = {    

   

 

preload: function()

       {                                       

        //-----to load objects and units before we begin our game

        this.load.image('preloadbar', 'images/loader_bar.png');         

        this.load.image('titleimage', 'images/TitleImage.png');

       },

 

create: function() 

        {

 

this.input.addPointer();

this.stage.backgroundColor = '#171642';

 

        this.state.start('Preloader');     // launches preloader from Boot.js         

         }

 

    };  

 

 

Here from what I had learnt about javascript prototyping was that , to add any method to an object or constructor function we used the following syntax/example:

 

     function employee(name,jobtitle,born)

      {

         this.name=name;

         this.jobtitle=jobtitle;

         this.born=born;

      }

 

     var fred=new employee("Fred Flintstone","Caveman",1970);

     employee.prototype.salary=null;

     fred.salary=20000;

 

 

Please help !!!

 

Link to comment
Share on other sites

BunnyDefender.Boot in this case is how you'd typically write a state. The employee example you provided is a more generalised example of creating an object with properties and functions. The style of coding in the state doesn't really matter, and both of the following will work:

BunnyDefender.Boot.prototype = {  method: function() {    // this is a method  }}// this will do the same as the aboveBunnyDefender.Boot.prototype.method = function() {  // this is a method}

The caveat is that the first method will totally replace the prototype with the specified functions and properties, which is fine when creating new objects - but if you want to extend or add to an existing object, use the second method, as then you're individually adding new properties and functions to the prototype rather than overwriting them all in one fell swoop.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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