Pages

Friday, November 08, 2013

Constructor Pattern

In object oriented languages, constructor is a special method used for initializing an object once it is created and allocated memory for that. In JavaScript, as almost everything is an object, we're most often interested in object constructors.

Object constructors are used to create specific types of objects - both preparing the object for use and accepting arguments which a constructor can use to set the values of member properties and methods when the object is first created.

Object Creation

Three common ways to create JavaScript objects are:
  1. var newObject = {};
  2. var newObject = Object.create(null);
  3. var newObject = new Object();
Where the "Object" constructor in the final example creates an object wrapper for a specific value, or where no value is passed, it will create an empty object and return it.var newObject = new Object();

No comments:

Post a Comment