Constructor function (Definition)

A function that can create new objects.

Availability:

ECMAScript edition - 2

A constructor is analogous to a factory class in a truly object-oriented system. It instantiates new objects of its class by copying a built-in prototype object.

A constructor function is a method that creates and initializes new objects or values. It is a way of calling the constructor object as a function rather than with the new operator.

These constructor functions are available from the global object; the host implementation may add others for you to use:

Calling the constructor as a function is a way of carrying out type conversion.

You can also create your own constructor functions. You name them with the name of the class you want to create. Depending on how you implement them, they may or may not work as type converting functions.

When we use the new operator, it understands that the function is a constructor and as it constructs the new object, it associates the constructor function with it so you can locate it again, via the prototype and constructor properties. Because this function makes use of the 'this' keyword, it could be a method belonging to another object. However, it doesn't have to belong to an object to be used as a constructor in the first place. The object creation process will properly associate it in due course when it needs to.

You don't need to pass parameters to add properties to objects as they are created.

Constructors are generally a better way of making self documenting objects than simply instantiating more copies of the Object object. In addition, there is more opportunity to reuse and share code between multiple instances. It's probably not a good thing to add properties and methods to the base Object class as it means they would get inherited just about everywhere, because ultimately all prototype inheritance chains descend from the topmost Object.

With a constructor, you can simulate arrays by making them from objects and property components. This may be useful if you want to run an array-based script in a very old JavaScript implementation although these days that likelihood of that is diminishing rapidly.

The Netscape browser creates a constructor for virtually every object it instantiates. This can be an aid to debugging and making more flexible scripts. You can inspect an object by requesting its constructor property. This will normally convert to a string that contains the function definition and to determine the class it may be more useful to request the constructor.name property instead.

This technique is not so useful in MSIE where constructors are only made available for objects that can genuinely be instantiated usefully by a script.

Example code:

   // This constructor function defines a class called Tree:
   function Tree(aName, aNode1, aNode2)
   {
      this.name        = aName;
      this.leftbranch  = aNode1;
      this.rightbranch = aNode2;
   }
   
   // We can now implement tree walking algorithms and
   // associate them with the prototype for the tree object:
   function tree_walk()
   {
      if((this.leftbranch  == null) &&
         (this.rightbranch == null))
      {
         document.write(this.name);
         document.write("<BR>");
      }
      if(this.leftbranch != null)
      {
         document.write(this.name);
         document.write(" -L- ");
         this.leftbranch.walk();
      }
      if(this.rightbranch != null)
      {
         document.write(this.name);
         document.write(" -R- ");
         this.rightbranch.walk();
      }
   }
   
   // Now associate the tree walk with the prototype.
   Tree.prototype.walk = tree_walk;
   // Here we create a new tree object:
   // In this case, we have defined null values to signify we have reached
   // the end of the branching structure so we must be at a leaf node.
   myTree1 = new Tree("AAA", null, null);
   // Let's create another and join both to a third:
   myTree2 = new Tree("BBB", null, null);
   myTree3 = new Tree("CCC", myTree1, myTree2);
   // Now we walk the tree
   myTree3.walk();
   // We could have created an array in the tree class and
   // stored more than two branches.  B-Trees, Quad trees,
   // and Oct trees are all useful modelling tools for
   // building simulations.

See also:Array simulation, Construct, constructor property, prototype property

Cross-references:

ECMA 262 edition 2 - section - 4.3.4

ECMA 262 edition 2 - section - 15.1.3

ECMA 262 edition 3 - section - 4.3.4

Wrox Instant JavaScript - page - 31