Constructor Pattern

Description

Constructors in Javascript creates specific types of objects.

We can create constructors that define properties and methods for custom object.

Example


function Person(name, age, job){/*www  .ja  v  a 2  s . co  m*/
   this.name = name;
   this.age = age;
   this.job = job;
   this.sayName = function(){
       console.log(this.name);
   };
}

var person1 = new Person("XML", 29, "Markup");
var person2 = new Person("CSS", 27, "Style");
console.log(person1.name);
console.log(person1.age);
console.log(person1.job);
person1.sayName();

console.log(person2.name);
console.log(person2.age);
console.log(person2.job);
person2.sayName();

The code above generates the following result.

Note

  • There is no object being created explicitly in the constructor.
  • The properties and method are assigned directly onto the this object.
  • There is no return statement.
  • By convention, constructor functions always begin with an uppercase letter, whereas nonconstructor functions begin with a lowercase letter.
  • To create a new instance of Person, use the new operator.
  • Constructors defined in this manner are defined on the Global object.

Constructor Property

Each of objects created by constructor has a constructor property that points back to Person.


function Person(name, age, job){/* w  w  w .  j  a va 2  s  .com*/
   this.name = name;
   this.age = age;
   this.job = job;
   this.sayName = function(){
       console.log(this.name);
   };
}

var person1 = new Person("XML", 29, "Markup");
var person2 = new Person("CSS", 27, "Style");
console.log(person1.constructor == Person);  //true
console.log(person2.constructor == Person);  //true

The code above generates the following result.

instanceof

We can use instanceof operator the check the object type.


function Person(name, age, job){/* w  w w.  j  av  a2s .  co m*/
   this.name = name;
   this.age = age;
   this.job = job;
   this.sayName = function(){
       console.log(this.name);
   };
}

var person1 = new Person("XML", 29, "Markup");
var person2 = new Person("CSS", 27, "Style");
console.log(person1 instanceof Object);  //true
console.log(person1 instanceof Person);  //true
console.log(person2 instanceof Object);  //true
console.log(person2 instanceof Person);  //true

The code above generates the following result.

Problems with Constructors

Methods are created for each instance by constructors.

Both person1 and person2 have a method called sayName(), but those methods are not the same instance of Function.

It's possible to work around this limitation by moving the function definition outside of the constructor.


function Person(name, age, job){/* w  w w .jav  a2s  .c o  m*/
   this.name = name;
   this.age = age;
   this.job = job;
   this.sayName = sayName;
}

function sayName(){
   console.log(this.name);
}

var person1 = new Person("XML", 29, "Mark up");
var person2 = new Person("CSS", 27, "Style");




















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions