Archive for the ‘black belt’ Category.

How to make a singleton in javascript

[UPDATE : This post is outdated. Check out the new post on singletons.]

Singleton is one of the most common and easiest design pattern. In fact, a lot of designs are wrong just because the developer didn’t know about singletons and design patterns (I could have said : a lot of my designs were wrong just because I didn’t know about singletons and design patterns).

I was looking at the first three results for singleton javascript on Google and I was more than deceived by what they had to tell us. That’s why I write this article.

What is a singleton class?

A singleton is when your application needs to use just one instance of an object to centralize your access to it. Example, I have a list of cat names that can be used in various functions of an application. Sometimes, names are added or removed. I could use a global variable to the page containing the list. The problems are :

- It’s a global variable (global variables are bad).
- I don’t know when the list is created or initialized.
- If you reuse your class in another project, you will always need to define a global variable and instance it correctly.
- It’s ugly.

If I use a singleton pattern, the advantages are :

- The object is always correctly created.
- You don’t have to ask yourself questions about the state of the list or whatever. It just works!
- Another programmer can recognize the singleton pattern thus making it easier for him to work.

How to write a singleton class

Before writing a singleton, you need to know about creating objects and static functions.

  1. // I create the class
  2. function CatNames() {
  3.         this.names = new Array(); // The array that will contain the names
  4. }
  5.  
  6. CatNames.instance = null; // Will contain the one and only instance of the class
  7.  
  8. // This function ensures that I always use the same instance of the object
  9. CatNames.getInstance = function() {
  10.         if (CatNames.instance == null) {
  11.                 CatNames.instance = new CatNames();
  12.         }
  13.  
  14.         return CatNames.instance;
  15. }
  16.  
  17. // Function to add a cat name
  18. CatNames.prototype.add = function(catName) {
  19.         this.names.push(catName);
  20. }
  21.  
  22. // Function to remove the last cat name
  23. CatNames.prototype.removeLast = function() {
  24.         return this.names.pop();
  25. }
  26.  
  27. // Function to alert all cat names
  28. CatNames.prototype.alertAllCats = function() {
  29.         alert(this.names.join(","));
  30. }

I use it in these functions.

  1. function addThreeNames() {
  2.         // I use the one and only instance of the class (the magic of singletons)
  3.         var names = CatNames.getInstance();
  4.  
  5.         names.add("Mistigri");
  6.         names.add("Bibi");
  7.         names.add("Gary");
  8. }
  9.  
  10. function removeLastCat() {
  11.         // Once again, singleton is here
  12.         var names = CatNames.getInstance();
  13.  
  14.         names.removeLast();
  15. }
  16.  
  17. // I call the functions
  18. // – no need for a global variable
  19. // – no need to create the object and pass it as parameter
  20. addThreeNames();
  21. removeLastCat();
  22.  
  23. // Alert all cats
  24. var names = CatNames.getInstance();
  25. names.alertAllCats();

The downside of this technique

Yes I know, you can instantiate the singleton class if you want.

  1. var catNames = new CatNames();

You know what? You’ll have to follow a simple rule : never instantiate a singleton class. Maybe we could use more techniques that would protect you from instantiating your own singleton class but it would be a waste of time and code.

Looping through object properties and hash tables using the “in” statement

As I said earlier, objects are hash tables of properties and functions so looping throught them is done the same way (weepee!).

The “in” statement

The in statement must be used in a for loop. Like this : for (var element in allElements). In most of the other languages, the element variable would contain the complete element but in javascript, it contains the name that refers to the complete element.

Looping through a hash table

Before doing it, maybe you should take a look at How to use javascript hashes (or hash-table) to make sure you fully understand what are javascript hash tables.

[source:javascript]

// I create a hash table
var hashPetNames = new Object();

// I create the pets
hashPetNames["cat"] = “Mistigri”;
hashPetNames["dog"] = “Rex”;
hashPetNames["bird"] = “Bibi”;

// I loop using the “in” statement
for (var name in hashPetNames) {
alert(“My ” + name + “‘s name is ” + hashPetNames[name]);
}

[/source]

Run this code and you should see the alerts :
- My cat‘s name is Mistigri.
- My dog‘s name is Rex.
- My bird‘s name is Bibi.

So easy.

Looping through an object

This one is a little kinkier. Why would I want to loop through an object properties and methods? I can’t give you an answer. It’s up to you to find it. I’ve used it a couple of times so it’s not as useless as you could think.

Example, I would like to separatly alert all the properties and methods of an object.

[source:javascript]

// Create the class
function TheClass() {
this.name = “The class”;
this.fullName = “The full name of the class”;
this.age = “51″;
}

// Add a function
TheClass.prototype.alertClass = function() {
alert(this.name + ” ” + this.fullName + this.Age);
}

// Construct the object
var theObject = new TheClass();

// Loop through the properties/functions
var properties = “”;
for (var propertyName in theObject) {
// Check if it’s NOT a function
if (!(theObject[propertyName] instanceof Function)) {
properties += propertyName + “, “;
}
}

alert(“Properties : ” + properties);

// Loop through the properties/functions
var functions = “”;
for (var functionName in theObject) {
// Check if it’s a function
if (theObject[functionName] instanceof Function) {
functions += functionName + “, “;
}
}

alert(“Functions : ” + functions);

[/source]

How to use javascript hashes (or hash-table)

Javascript has no hash table object. In fact, all objects are hashes.

  1. var hashPetName = new Object();
  2. hashPetName["dog"] = "Rex";
  3. hashPetName["cat"] = "Mistigri";
  4.  
  5. console.log("My dog’s name is " + hashPetName["dog"]
  6.         + " and my cat’s name is " + hashPetName["cat"] );
  7.  

It’s a little overwhelming to write it like that so let’s use a simpler form.

  1. var hashPetName = {
  2.   dog: "Rex",
  3.   cat: "Mistigri"
  4. }
  5.  
  6. console.log("My dog’s name is " + hashPetName["dog"]
  7.         + " and my cat’s name is " + hashPetName["cat"] );
  8.  

Great! That’s it about javascript hashes, now I can go groom my cat.

Wait!!!

Why can I use an object to emulate a hash? Can I do the same thing with a Number object? Let’s try with the same code than above but with a Number instead of an Object.

[source:javascript]
var hashPetName = new Number(); // Magic is here!
hashPetName["dog"] = “Rex”;
hashPetName["cat"] = “Mistigri”;

alert(“My dog’s name is ” + hashPetName["dog"]
+ ” and my cat’s name is ” + hashPetName["cat"] );
[/source]

It works!!! What can we understand of this?

All objects are hashes too

When you are javascripting, forget everything you know about other languages, it won’t be any help.

I’m adding to the list

  • All objects are hashes

When we create an object, its properties and methods are added to the hash of the object.

[source:javascript]
// Create a class
function TheClass() {
// Adding a property
this.name = “the name”;
}

// Create the object
theObject = new TheClass();

// Alert the property name
alert(theObject.name);

// Alert the property name using the hash
alert(theObject["name"]);
[/source]

In this example, you see that using theObject.theProperty is the same thing that using theObject["theProperty"].

Dynamically add a property/method to the object

In my first example, I didn’t create the property in the constructor before using it. As you saw, it’s easy to dynamically add a property or a method.

[source:javascript]
// Create a class
function TheClass() {
}

var theObject = new TheClass();

theObject["age"] = 3; // Create a new ‘age’ property
theObject.fullName = “Mistigri”; // Create a new ‘fullName’ property

theObject.alertDetail = function() {
alert(this["fullName"] + ” is ” + this.age);
}

theObject["alertDetail"]();
[/source]

Beware! The new property/method will only be available to the instance you’re working on. If you want to add a new property/method to every instances of the class, use prototypes.

Using an array thinking it’s a hash

One of the most common mistake I see is to use an Array object thinking that’s the only one that you can use the brackets to create a hash. Even if it works, it’s not how it should be done. I recommend the use of Object instead.

[Update : A friend of mine pointed me out that I should have talked about hash tables (or associative arrays) instead of hashes.]

How to do class functions in javascript (aka static or shared functions)

One of the really concept of the object-oriented programming is the class functions but they are also known as static functions (Java, php, C++) or shared functions (.NET).

Why using class method?

Class methods help you keeping your code clean by centralizing your functions. Example, you create a calendar object that formats a string from two dates to have readable time difference (example, input : “2007-08-13″ and “2007-08-16″, output : “begins august 13th 2007 and ends 3 days later”).

Sure you can create a readableTime(beginDate, endDate) function. First problem is that there may be another function with that name. Second problem? If another programmer works on the code that makes a call to readableTime function, how does he knows that it comes from the javascript file containing the Calendar object?

So, you could create a Calendar_readableTime(beginDate, endDate) function. Great! But what if the object name Calendar changes for BestCalendar? The Calendar_readableTime function won’t have the good suffix.

The solution is to use a class function so the call to the readableTime function will look like this : Calendar.readableTime(beginDate, endDate). Nice looking and clean code.

How to create a class function

First, you create a class

[source:javascript]
function Calendar() {
}
[/source]

Second, you add the class function

[source:javascript]
function Calendar() {
}

Calendar.readableTime = function() {
// Code here
}
[/source]

Then, it’s ready to be called in your code

[source:javascript]
var cal = new Calendar();
var beginDate = new Date();
var endDate = new Date();
endDate.setDate(endDate.getDate() + 3); // Add three days to endDate

var stringTime = Calendar.readableTime(beginDate, endDate);
[/source]

Difference with other languages

In many other languages, your class functions are callable from an instantiated object like this.

[source:javascript]
var cal = new Calendar();
var beginDate = new Date();
var endDate = new Date();
endDate.setDate(endDate.getDate() + 3); // Add three days to endDate

// I call cal.readableTime and it doesn’t work!
var stringTime = cal.readableTime(beginDate, endDate);
[/source]

In javascript, it doesn’t work because the function readableTime is assigned to the class only and is not referenced in the instances of the class.

To give the access to the readableTime function on the instance of an object is to create the function this way :

[source:javascript]
function Calendar() {
}

Calendar.readableTime = new function() {
// Code here
}

// Here’s the magic
Calendar.prototype.readableTime = Calendar.readableTime;
[/source]

By assigning the function to the prototype, each object of the Calendar type will have the readableTime function.

How to use anonymous objects

I don’t know how to call it and I don’t even know if it has a name. I thought a moment about calling it Mistigri but I already have a cat with that name. I decided to call it anonymous object.

How?

[source:javascript]

var ao = {
name : “Anonymous Object”,
possibleName : “Mistigri”,
presentYourself : function() {
alert(“Hi! My name is ” + this.name + ” but they thought of calling me ”
+ this.possibleName + “.”);
}

ao.presentYourself();

[/source]

By running the code below, you should see a message box with Hi! My name is Anonymous Object but they thought of calling me Mistigri. Isn’t it wonderful?

What can I do with that?

For the moment, I haven’t found a lot of uses for anonymous objects. If you know more, leave a comment and I will add yours to mine. Maybe we’ll end up with two and a half diffrent uses.

Still, I am using it to create enumerations (enum).
[source:javascript]
var DisplayType = {
big : 0,
medium : 1,
small : 2
}

var display = DisplayType.big;
alert(display);
[/source]

This is very useful instead of creating “constants” with different name.

[source:javascript]

var DISPLAY_TYPE_BIG = 0;
var DISPLAY_TYPE_MEDIUM = 1;
var DISPLAY_TYPE_SMALL = 2;

[/source]

Second use

Nicolás Sanguinetti pointed to me another use of anonymous objects.

Saying you have a lot of possible parameters to pass to a method. Instead of having them all list, you use an anonymous object as parameters and the programmer decides which one he will use.

Instead of

[source:javascript]
function doSomething(id, name, label, age, fatherName, brotherName, motherName, grandmaName, isFunny, isShy)
{
var concat = id + ” ” + label + ” ” + isFunny;

// More code…
}

doSomething(2, null, “name”, null, null, null, null, null, true, null);
[/source]

you have

[source:javascript]
function doSomething(params)
{
var concat = params.id + ” ” + params.label + ” ” + params.isFunny;

// More code…
}

doSomething({id : 2, label : “name”, isFunny: true});
[/source]

How to do enumerations (enum) in javascript

One of the most common mistake I see when I use a third-party javascript library is the enormous amount of “constants” that is 152 characters to ensure its uniqueness. Example, a color chooser named KikaColorChooser. If it has a constant for the type of display, it will be named something like KIKACOLORCHOOSER_DISPLAY_TYPE_SMALL (a constant of 35 characters long). Ugly.

The second most common mistake is to have a short constant name that have dozens of friends with the same name. DISPLAY_SMALL is a good example.

The solution?

[source:javascript]

// The class
function KikaColorChooser() {
}

// …implementation here…

// The display type
KikaColorChooser.displayType = {
small : 0,
big : 1
}

[/source]
Thanks to the power of anonymous objects.

The downside

After writing this article, I remarked that my first argument could not stand.

I had the constant KIKACOLORCHOOSER_DISPLAY_TYPE_SMALL and the new solution is KikaColorChooser.displayType.small. The old solution is 35 characters long and the new one, 34. And no you can’t shorten the new solution in the class by just writing this.displayType.small.

Still, you should do it just for the cleanliness of your code and to impress your friends.

How to extend javascript classes

The intrinsec objects of javascript (String, Number, Date, etc) are missing a lot of handy methods. God knows why. Example, you don’t have a trim() function on a String object. Maybe the developers thought that it was easy enough to write theString.replace(/^\s*|\s*$/g, “”) to trim a string but that’s not the kind of ugly code I want to see everywhere in my projects. It’s unesthetical. To do this, I have to use prototype.

So I want to add a trim() method to all my objects that are String class.

[source:javascript]

String.prototype.trim = function()
{
return this.replace(/^\s*|\s*$/g, “”);
}

[/source]

Easy.

Special note : I don’t want you to copy/paste that code. It’s for the purpose of the demonstration only.

I can do the same thing for every objects wheter they are intrinsic javascript classes, third-party classes or my own classes.

What are javascript prototypes? (short answer for advanced javascripters)

[I've also written a longer answer for beginners] 

Prototypes can extend any class you want by adding a property or a method. By calling,

[source:javascript]
String.prototype.alertMe = function() {
alert(this);
}
[/source]

you are adding the method alertMe() to every String object of your application.

It uses less memory because javascript creates only one instance of the function and uses references to it.

Short answer done!