Home >Web Front-end >JS Tutorial >In-depth understanding of JavaScript series (26): Detailed explanation of the constructor pattern of design patterns_javascript skills
Introduction
Everyone is familiar with constructors, but if you are a novice, it is still necessary to understand what a constructor is. A constructor is used to create an object of a specific type - not only does it declare the object to be used, the constructor can also accept parameters to set the object's member values when the object is first created. You can customize your own constructor and declare properties or methods of custom type objects in it.
Basic usage
In JavaScript, constructors are usually used to implement instances. JavaScript does not have the concept of classes, but there are special constructors. By calling the defined function using the new keyword, you can tell JavaScript that you want to create a new object and the member declarations of the new object are all defined in the constructor. Inside the constructor, the this keyword refers to the newly created object. The basic usage is as follows:
var tom= new Car("Uncle", 2009, 20000);
var dudu= new Car("Dudu", 2010, 5000);
console.log(tom.output());
console.log(dudu.output());
The above example is a very simple constructor pattern, but there are a few problems. First of all, using inheritance is very troublesome. Secondly, output() is redefined every time an object is created. The best way is to let all instances of the Car type share this output() method, so that if there are a large number of instances If so, it will save a lot of memory.
To solve this problem, we can use the following methods:
function formatCar() {
Return this.model "Gone" this.miles "Kilometers";
}
Constructor and prototype
The function in JavaScript has a prototype attribute called prototype. When a constructor is called to create an object, all the attributes of the constructor prototype are available on the newly created object. According to this, multiple Car object instances can share the same prototype. Let’s expand the code of the above example:
/*
Note: Here we use Object.prototype. method name instead of Object.prototype
Mainly used to avoid rewriting the defined prototype prototype object
*/
Car.prototype.output= function () {
Return this.model "Gone" this.miles "Kilometers";
};
var tom = new Car("Uncle", 2009, 20000);
var dudu = new Car("Dudu", 2010, 5000);
console.log(tom.output());
console.log(dudu.output());
Also: We recommend that constructors start with a capital letter to distinguish them from ordinary functions.
Can I only use new?
The above examples all use new to create objects for the function car. Is this the only way? In fact, there are other ways, we list two:
//Method 1: Call as a function
Car("Uncle", 2009, 20000); //Add to window object
console.log(window.output());
//Method 2: Call within the scope of another object
var o = new Object();
Car.call(o, "Dudu", 2010, 5000);
console.log(o.output());
Force new
The above example shows the problem of not using new, so is there any way for us to force the constructor to use the new keyword? The answer is yes, the above code:
var tom = new Car("Uncle", 2009, 20000);
var dudu = Car("Dudu", 2010, 5000);
console.log(typeof tom); // "object"
console.log(tom.output()); // "Uncle has walked 20,000 kilometers"
console.log(typeof dudu); // "object"
console.log(dudu.output()); // "Dudu walked 5000 kilometers"
Original wrapper function
There are three primitive wrapper functions in JavaScript: number, string, boolean. Sometimes both are used:
// This is recommended
var s = "my string";
var n = 101;
var b = true;
//original string
var greet = new String("Hello there");
// Split
using split() method
greet.split(' ')[0]; // "Hello"
// Adding new attributes to the wrapper function type will not cause an error
greet.smile = true;
// New properties can be accessed normally
console.log(typeof greet.smile); // "boolean"
Summary
This chapter mainly explains how to use the constructor pattern, how to call it, and the difference between the new keyword. I hope everyone will pay attention to it when using it.
Reference: http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#constructorpatternjavascript