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

The constructor type

In a previous article the idea of object factories and constructors was introduced. At the level of just building objects there is little difference between an object factory and a constructor - they appear to be syntactic variations on getting the same job done. However, the constructor approach has some tricks hidden about its person and the first is the implementation of a "type" system.

If an object is created by a constructor then, as long as you don't modify it by adding or removing properties and/or methods then you could consider all objects created by the constructor to be of the same type.

That is, all objects created by a constructor can be considered, initially at least,  to have the same set of properties and methods and so represent the same type.

You might object that as a constructor can create an object in any way it cares to even this conclusion isn't strictly true. For example, consider this odd definition of randomPoint:

function randomPoint()
{
 this.x=0;
this.y=0;
if(Math.random()>.5)this.z=0;
}

var point=new randomPoint();
alert(point.z);

Half of the time point has a z property and half it doesn't. It has a constructor but it certainly doesn't represent a single type in the traditional sense of the word.

Partly pathological examples excepted, the constructor is still the best guide to what properties and methods an object has.

For this reason the Javascript "type" system focuses on the constructor. The rule is that if an object has been created using a constructor called MyType then it is regarded as being an instance of MyType.

You can see that this is a very weak conclusion and not at all like the same statement in a strictly typed class based language. It is very important that you see Javascript's implementation of type in this way. It is deliberately weak and really only introduced to allow certain procedures that would be difficult without it.

The constructor property

So type is based on the constructor, if any, used to create the object. To allow type checking every object has a built-in constructor property which is automatically set to reference its constructor.

If you create an object literal then the object's constructor property is set to reference the Object constructor function even though you might not have used it to create the literal. That is, if you try:

var o={};
alert(o.constructor);

you will see that the constructor is Object(). Similarly if you try:

var o=new Object();
alert(o.constructor);

you will once again find that the constructor is Object.

However, if you create an object using a custom constructor then you will find that the object's constructor property is a reference to it.

For example:

function Point(){
this.x = 0;
this.y = 0;
this.setPoint = function(x, y){
this.x = x;
this.y = y
};
}
var point=new Point();
alert(point.constructor);

You will discover that the constructor property is set to reference the Point function object that created the object.

What use is the constructor property?

Its main use in simple Javascript programming is with the Instanceof operator. This compares the constructor property to whatever you want to compare it to and returns true or false.

For example following the above definition for Point:

var point=new Point();
alert(point instanceof Point);

will display true.

The instanceof expression can be thought of as:

alert(point.constructor==Point);

but because of considerations related to the prototype property the way that instanceof works is slightly more complicated than this.

The importance of the instanceof operator is that it encourages us to think that the point object is of type Point. This then encourages us to believe that Javascript supports a type system - it doesn't.

When you write

object instanceof function

you are simply testing to see it object was constructed by function.

Banner

<ASIN:0137054890>

<ASIN:1449381871>

<ASIN:0596806752>



Last Updated ( Monday, 04 October 2010 )