Javascript Jems - Type and the constructor
Written by Ian Elliot   
Thursday, 23 September 2010
Article Index
Javascript Jems - Type and the constructor
Constructor type
Object factories as constructors

Banner

 

Testing which function created an object can be useful if you are trying to build a mechanism for restricting what people can do with objects you provide them with. It allows you to implement some of the features of a type system by regarding type as being determined by the constructor.

For example in the previous article the idea of using instanceof to test it this was correctly set was described:

function Point(){
if(!(this instanceof Point))
                    return new Point();
this.x = 0;
this.y = 0;
this.setPoint = function(x, y){
this.x = x;
this.y = y
};

If Point is used with new this will be set to reference the anonymous object that is being constructed and the anonymous object's constructor property will already have been automatically set to reference the Point function. Notice that this is not a reference to Point.

A more transparent, but not identical, way of writing this function is:

function Point(){
if(!(this.constructor==Point))
                     return new Point();
this.x = 0;
this.y = 0;
this.setPoint = function(x, y){
this.x = x;
this.y = y
};

Finally notice that there is also a typeof operator but it returns "object" for every custom object and its main role in life is to let you identify primitive datatypes.and built-in objects.

Object factories as constructors

You may at this point be wondering if object factories can be regarded as constructors? The answer is yes.. and no. There is nothing stopping you from setting the constructor property of any given object to anything you like. This might be seen as a disadvantage of the system because it looks as if you can fool the Javascript type system in this way - as it turns out you can't, but more on this later.

So to assign an object factory as the constructor of an object you would use something like:

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

Notice the assignment to the constructor property. Now if you try:

var point= NewPoint();
alert(point.constructor);
alert(point.constructor==NewPoint) ;

you will discover that NewPoint is indeed the constructor of point and the test evaluates to true. However if you now try:

alert(point instanceof NewPoint);

you will, probably, be surprised to see false displayed.

The reason is that instanceof actually uses a more complicated way of determining if the given function was used in creating an object than it just being named as the constructor. To be more precise it makes use of the objects prototype chain but exactly how this works takes us into Javascript's form of inheritance.

Finally notice that if you have an object you can use the constructor property to get another one. For example:

var point= NewPoint();
var point2=new point.constructor();

Naming constructors

So the bottom line is that Javascript regards and object's type and its constructor function as one and the same.  What this means is that if you are going to test for an objects "type" using instanceof you have to use sensible names for constructors and you have to use constructors rather than object factories. In particular you don't want to call a constructor something like:

function CreatePoint(){...

because then you have to write

if( mypoint instanceof CreatePoint )...

So much better to use a constructor called Point and then be able to write

if( mypoint instanceof Point)...

Notice that we are slowly but surely starting to regard the constructor function object as a surrogate class for the object it creates.

This is a shame and misses the elegance of the way that Javascript does object.

The next topic we have to deal with is prototype inheritance and this more or less concludes our look at the way Javascript does objects.

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook , on Digg or you can subscribe to our weekly newsletter.

<ASIN:0137054890>

<ASIN:0321572602>

<ASIN:0470684143>

Banner


JavaScript Jems - The Inheritance Tax

JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way.  In particular, it doesn't do inheritance  [ ... ]



JavaScript Jems - Objects Are Anonymous Singletons

JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way.  In particular, every object can be regard [ ... ]


Other Articles



Last Updated ( Monday, 04 October 2010 )