|Wednesday, 15 September 2010|
Page 1 of 3
In Part One we looked at the basics of object creation using the object literal. This is a good way to create small numbers of objects but now it is time to turn our attention to what we do when we want multiple instances of an object - this is the topic of object factories and constructors but to be complete we also look at object cloning.
The object factory
In the pprevious article we discovered that you can build an object very simply by creating an empyt object literal and then adding methods and properties to it dynamically. Manual object construction is fine when you want a one off object but what do you do when you want a lot of instances? The answer is we do what we always do - we code it. That is the automated way to object construction is to write code that creates the object.
What is the best way to package this code?
As a function of course.
So how do we create an object factory?.
At the end of the first part we encountered the point object:
and created a for loop to generate lots of instances.
Instead of taking this direct approach let's build an object factory that makes points.
Notice that the function simply returns a reference to an anonymous object literal. Also notice that the this used in the object literal is set to the current context i.e. the anonymous object literal itself.
This is all there is to an object factory - build an object in the usual way and return a reference to it.
To use the object factory you simply write something like:
and then use point as if it was a perfectly ordinary object literal, e.g.
Also notice that an object factory can be a property of another object. That is you can have a master object in your project which provides all of the object factories it needs. This is the approach used by the Google Earth API for example which has lots of CreateObject methods.
Of course once we have an object factory we can use to to stamp out lots of instances of the object. For example:
var Points=new Array(10);
There are other ways of writing an object factory other than to define everything within the object literal. For example you can do the complete job dynamically:
In this case we create and object literal and store a reference to it in object. After this we can add properties and methods in the usual way. At the end we return the reference to the object literal as before.
Notice that we don't use this in this case. The reason is that this has the usual context for a function call i.e. the global context as the function isn't a property of another object and therefore isn't called in the context of another object.
This dynamic style of object creation within a function is preferred by many programmers because it uses a more familiar syntax. For example, you can create and initialise simple properties using assignment. However the price of this syntactic simplicity is that you have to explicitly use the object reference in everything you define.
|Last Updated ( Monday, 04 October 2010 )|