Javascript Jems - Object factories, constructors and clones
Written by Ian Elliot   
Wednesday, 15 September 2010
Article Index
Javascript Jems - Object factories, constructors and clones
Parameters and constructors
Cloning

Banner

Parameters

There is no reason why you can't pass some parameters to the object factory.

In most cases these are typically initialisation parameters of the sort that would be used in a class constructor.

For example:

function NewPoint(x,y)
{
var object={};
 object.x=x;
object.y=y;
 object.setPoint= function(x, y){
  object.x = x;
  object.y = y
}
return object;
};

Now the object factory can be used to initialise the point objects it creates as in:

Point = NewPoint (0,0);

Of course you can use the usual techniques to allow defaults and optional parameters. For example:

function NewPoint(x,y)
{
var object={};
object.x=x||0;
object.y=y||0;
 object.setPoint= function(x, y){
  object.x = x;
  object.y = y
}
return object;
};

As well as using initialisers in the object factory you can also go beyond what is usually possible in a class based language and pass parameters that change the structure of the object.

For example, suppose you sometimes wanted a 3D point but when a point was 2D you didn't want it to have a z component:

function NewPoint(x,y,z)
{
 var object={};
 if(z!=null)
 {
   object.z=z;
 }

This creates a z property if and only if a value of z is specified in the function call.

Even more radical in appearance is the way parameters can customise the creation of methods. For example, suppose you wanted the setPoint method to include a z parameter only if the point was a 3D point you might use:

if(z!=null)
{
object.setPoint= function(x, y,z){
  object.x = x;
  object.y = y
 }
}else{
object.setPoint= function(x, y){
  object.x = x;
  object.y = y
 }
}

Once you get the idea of conditional object construction you can think up lots of examples.

Unlike a traditional class-based object-oriented approach an object factory can build an object to your specifications at run time not just at design time.

Constructors

Now that you have seen an object factory in action you might be wondering what a constructor is all about?

Why do we need anything extra over and above an object factory?

There are number of reasons why something just a little bit more sophisticated is required - but the main one is efficiency. This is a topic that will be described in detail in later articles so for now just take it on trust that constructors allow object to be created in ways that are more efficient and slightly more flexibly than object factories.

For the moment concentrate on the simpler syntax provided by a constructor.

The need to use object factories is obvious - so obvious that Javascript has the keyword new to make writing functions that create objects simpler.

When you place new in front of a function call, any function call, two extra things happen. (In fact there is a third thing that happens but more on this when we look at prototypes.)

The first is that an anonymous object is created and this is set to reference it. That is, it is as if the command:

this={};

is carried out just before the function is called. The second is that this is returned as the value of the function if you don't explicitly return a value.  That is, it is as if the command:

return this;

is automatically added to the end of the function. Notice that these two things happen irrespective of the form of the function. For example, if you create an empty function:

function myFunction(){}

and use:

var myObject= myFunction();

then you will find that myObject is undefined - the function doesn't return a value. However, if you write:

var myObject= new myFunction();

then you will discover that myObject refernces an object, i.e. the object that the new created and that this was set to reference.

A constructor based version of the point object factory is simply:

function Point()
{
this.x=0;
this.y=0;
this.setPoint = function(x, y){
  this.x = x;
  this.y = y
 }
}

and you can clearly see the differences. There is now no need to create an object literal - one is automatically created. There is also no need to explicitly create a variable to reference the anonymous object literal - this is automatically set to reference it. And finally there is no need to write:

return this;

as the last instruction - although you can if you want to without changing the behavior of the constructor.

You can see that the constructor approach to building objects is just as powerful as the object factory and just a little easier syntactically. In fact it has another big advantage to do with efficiency but more of this in the next article. For the moment, however, it is worth stating that as far as efficiency goes there is little difference between the two as they basically implement the same mechanism.

Banner

<ASIN:0596517742>

<ASIN:0321572602>

<ASIN:0470526912>

<ASIN:1590597273>



Last Updated ( Monday, 04 October 2010 )