Sign in to follow this  
phaserNOOB

function prototyping ?

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

 

Share this post


Link to post
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.

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.