Play the game at www.RainingChain.com.
Check the source code on Github.

Monday, 9 March 2015

Static Class vs Prototype

There are many ways to create object constructors in a project. The most conventional way is to use prototypes. Even though they are the most performant, they also have disadvantages and restrictions. For example, the use of "this" keyword in prototypes prevents multi-hierarchic function names such as Actor.prototype.status.update(). This is normally not a big deal though.

Like I said in another post, my project used to be entirely made with global variables. Obviously, I wasn't using a proper constructors system back then and when I made the transition to make the project module-based, I simply converted all my global functions as the equivalent of public static functions that always take the concerned object as the first parameter.

//Old (Global Variables)
updateActorStatus = function(actor){ actor.hp++; }

//New (Module-Based)
Actor.status.update = function(actor){ actor.hp++; }

//With prototypes it would look like:
Actor.prototype.updateStatus = function(){ this.hp++; }

The main inconvenient with static functions is that it complicates heritages. But considering Javascript is not strictly typed and the game was built with minimal heritage in the first place
makes this not really worth the trouble.

Then there's performance, but the constructors are not a big part of the CPU usage in the game.
I'd rather spend my time optimizing the collision, movement and boost systems.

In the other hand, static functions have a considerable advantage. It makes it very easy to search for a specific function. For example, if I want to know what files are impacted by the modification of the function Actor.remove, I can simply search for Actor.remove(.

However, if using prototype, the function is Actor.prototype.remove. Searching for .remove( will returns results concerning Actor but also drop.remove, bullet.remove, boost.remove, ability.remove, equip.remove, item.remove, map.remove etc...

Another advatange of static functions is that it enforces a proper dependencies system since you absolutely need the module Actor to call Actor.status.update. In the other hand, calling actor.updateStatus can be done without the module since the function comes with the object.

3 comments:

  1. The best thing about HTML5 is that it allows the developers to embed the video files, audio files, and high quality graphics without any third party applications.
    Fita Chennai reviews | html5 training institutes in chennai

    ReplyDelete
  2. Dot net is a Microsoft product so it is the best language to develop applications for windows and it is supported well on the windows platform. Dot net is prefferd globally and a renowned platform with lots of job opportunities.
    Dot net training in Chennai | .NET training in Chennai | Dot net course in Chennai

    ReplyDelete