Skip to main content

Constructor Design Pattern

Creational: Constructor Pattern


Can you explain the constructor design pattern?

View Answer:
Interview Response: The constructor pattern is a design pattern that uses a class or function to create unique types of objects. A constructor is a one-of-a-kind method for initializing a newly created object after allocated memory.

Constructor patterns are among the most fundamental, extensively used, and modern JavaScript patterns. The objective of this pattern, as hinted by the name, is to facilitate constructor creation.


In the constructor pattern, what kinds of objects can we implement?

View Answer:
Interview Response: The two types of objects used in the constructor pattern include classes and traditional functions.

Code Example: Traditional "function" based syntax

//  a) Traditional "function" based syntax

function Person(name, age) {
this.name = name;
this.age = age;
this.getDetails = function () {
console.log(`${this.name} is ${this.age} years old!`);
};
}

Code Example: ES6 "class" syntax

// ES6 "class" syntax

class Person {
constructor(name, age) {
this.name = name;
this.age = age;
this.getDetails = function () {
console.log(`${this.name} is ${this.age} years old!`);
};
}
}

//Creating new instance of Person
const personOne = new Person('John', 20);
personOne.getDetails();

// Output - “John is 20 years old!”

In what pattern category does the Constructor pattern belong?

View Answer:
Interview Response: The Constructor pattern belongs to the creational design pattern category.

What ES6 object do we use in the constructor pattern?

View Answer:
Interview Response: The ES6 object used in the constructor pattern is a class object.

When should you use the constructor pattern?

View Answer:
Interview Response: You can use the Constructor Pattern when you want to create multiple instances of the same object. The constructor pattern is typical in libraries and plugins.

What are some of the constructor pattern and methods issues?

View Answer:
Interview Response: When building constructors, you should be aware of object duplication resulting in a new instance. The repetition can be caused by using methods when building prototypes and returning false results when checking instance equality.

Code Example:

function Phone(brand, model, countryDesignedIn, countryMadein) {
this.brand = brand;
this.model = model;
this.countryDesignedIn = countryDesignedIn;
this.countryMadein = countryMadein;

this.toString = function () {
return `${this.brand} ${this.model} manufactured in ${this.countryMadein}`;
};
}

Phone.prototype.toStringAlt = function () {
return `${this.brand} ${this.model} designed in ${this.countryDesignedIn}`;
};

yourPhone = new Phone('Nokia', '3310', 'Denmark', 'Denmark');
myPhone = new Phone('iPhone', '7', 'USA', 'China');

// Test if toString method works
console.log(yourPhone.toString()); // Output: Nokia 3310 manufactured in Denmark
console.log(myPhone.toString()); // Output: iPhone 7 manufactured in China

// Test if toString function are not duplicated (let's say the same object)
console.log(
`toString functions are the same object: ${
yourPhone.toString === myPhone.toString
}`
);

// Test if toStringAlt method works
console.log(yourPhone.toStringAlt()); // Output: Nokia 3310 designed in Denmark
console.log(myPhone.toStringAlt()); // Output: iPhone 7 designed in USA

// Test if toStringAlt function are not duplicated (let's say the same object)
console.log(
`toStringAlt functions are the same object: ${
yourPhone.toStringAlt === myPhone.toStringAlt
}`
);
// Output: toStringAlt functions are the same object: true

// Checking Instance Equality
console.log(yourPhone === myPhone); // false

What differentiates the constructor pattern from the prototype pattern?

View Answer:
Interview Response: The primary difference between the constructor and prototype pattern is the reusability of components.

Constructor Pattern:
When you create a new constructor, it creates a new instance of everything, and any changes made to the instantiated object do not affect the others.

Prototype Pattern:
Creating a new object using the prototype reuses the logic, and any change to the prototype chain affects everyone else.