Category Archives: Javascript

Namespaces and classes in Javascript

Facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

I am currently working on a game for Air Console. It’s a cool platform for local multiplayer web games.

So far most of my time has been spent experimenting with HTML5 game frameworks. For this project I have settled on the CreateJS suite. It seems to be functional, minimal and up to date.

I have also been learning a bit more about how to organize a HTML/javascript application. Javascript doesn’t have special keywords for ‘class’ or ‘namespace’. Everything is done using functions, which left me scratching my head and wondering how you’re supposed to manage a large codebase without constantly running into name conflicts.

There were two articles I found helpful. The first is a simple rundown of some common ways to use Javascript’s functions like a class:

The second article explains a slightly more complex concept that allows you to create namespaces within Javascript. This allows you to encapsulate all the variables in your app within a single root var, which helps reduce the chance that one of your variables is defined in another module.

The article features my new favourite piece of technical jargon: Self-executing anonymous function. It’s a phrase that would strike terror in a student’s heart if it appeared at the top of a lecture slide.