In Javascript design pattern term is used for a reusable solution for commonly occurring problems in Javascript. The constructor design pattern is a class-based design pattern. Constructors are special functions that can be used to instantiate new objects with methods and properties defined by that function. Constructor pattern is one of the most commonly used patterns in JavaScript for creating new objects of a given kind.
Example of Constructor Design Pattern
Input:
// using constructor function
let User = function(firstName, lastName, emailId, age){
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
this.age = age;
this.getFullName = function (){
return this.firstName + " " + this.lastName;
}
}
var user1 = new User('Ramesh', 'Fadatare', 'ramesh24@gmail.com', 29);
var user2 = new User('John', 'Cena', 'john@gmail.com', 45);
// Print objects
console.log(user1);
console.log(user2);
// access properties
console.log(user1.firstName);
console.log(user1.lastName);
console.log(user1.age);
// calling method
console.log(user1.getFullName());
Output:
User {firstName: "Ramesh", lastName: "Fadatare", emailId: "ramesh24@gmail.com", age: 29, getFullName: ƒ}
User {firstName: "John", lastName: "Cena", emailId: "john@gmail.com", age: 45, getFullName: ƒ}
Ramesh
Fadatare
29
Ramesh Fadatare
People having good knowledge of Financial accounting can get an Accounting Certification Exams from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to Financial accounting or you can go for advanced level certification if you have expert level skills in Financial accounting.