Sign in to follow this  

Interphase Issue 1, why define everyhing inside the prototype?

Recommended Posts


I am new to PhaserJS and I have been learning the basics with the Interphase book. I would like to know why is everything defined inside the prototype? Is this for avoiding performance issues? Common practice? Or because I am going in this way you can reuse the State several times in the game?

Also, what is the purpose of defining objects properties inside the function and then use them in the prototype? For example, see this.sprite and in the example below; is this a way of sharing the parameters across states?

var MyGame = {};MyGame.StateA = function (game) {    this.sprite = null; = null;};MyGame.StateA.prototype = {    preload: function () {},    create: function () {}};

Share this post

Link to post
Share on other sites

I can't speak for Phaser specifically, but the reason to define functions on the prototype in JavaScript is indeed performance related. Any object that extends from that prototype will share the same instance of the function, whereas if it were declared directly on the object a new instance would be created for each instance of that object type. This means you're saving both memory and whatever processing time it takes to create that function for each object.

Some could argue it's more semantic to group these on the prototype. 

In most cases this performance discrepancy isn't so important, but certainly could be in game development

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.

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.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.