This article describes the constructor mode of JavaScript programming design pattern. Share it for your reference, as follows:
In the classic OOP language, a constructor (also called a constructor) is a special method for initializing an object. In JS, because everything is an object, object constructors are often brought up.
The object constructor is used to create an object of the formulation type (Class) and can accept parameters to initialize the properties and methods of the object.
Object creation
In JS, there are three commonly used methods for creating objects:
//1, recommendedvar newObject = {}; //2, var newObject = ( null ); //3, not recommendedvar newObject = new Object();
However, this only creates three empty objects, without any properties or methods. We can set properties and methods for the object through the following four methods.
// ECMAScript 3 compatible way// 1. General object definition method//Set properties = "Hello World"; //Get attributevar key = ; // 2. Square bracket method// Set propertiesnewObject["someKey"] = "Hello World"; //Get attributevar key = newObject["someKey"]; // Only for ECMAScript 5// 3. // Set properties( newObject, "someKey", { value: "for more control of the property's behavior", writable: true, enumerable: true, configurable: true }); //The property settings can be simplified through the following functionsvar defineProp = function ( obj, key, value ){ = value; ( obj, key, config ); }; // How to usevar person = ( null );defineProp( person, "car", "Delorean" ); defineProp( person, "dateOfBirth", "1981" ); defineProp( person, "hasBeard", false ); // 4. //Set properties( newObject, { "someKey": { value: "Hello World", writable: true }, "anotherKey": { value: "Foo bar", writable: false } }); // The methods of obtaining attributes of 3 and 4 are the same as 1 and 2.
Basic constructor
We know that there is no concept of Class in JS, but it also supports building objects with constructors.
By using the [new] keyword, we can make a function behave like a constructor, thus creating our own object instance.
A basic constructor is as follows:
function Car( model, year, miles ) { //Here, this points to the newly created object itself = model; = year; = miles; = function () { return + " has done " + + " miles"; }; } //usage// Create two car instancesvar civic = new Car( "Honda Civic", 2009, 20000 ); var mondeo = new Car( "Ford Mondeo", 2010, 5000 ); // Output result( () ); ( () );
This is the simple constructor pattern, it has two main problems,
First, it is difficult to inherit; second, toString() is defined by each object instance, and as a function, it should be shared by each instance of Car type.
Using prototype constructor
There is a good feature in JS: Prototype,
Using it, when creating an object, all properties in the constructor prototype can be obtained by the object instance.
This way multiple object instances can share the same prototype.
Our previous example of Car improvement is as follows:
function Car( model, year, miles ) { = model; = year; = miles; } = function () { return + " has done " + + " miles"; }; // Usagevar civic = new Car( "Honda Civic", 2009, 20000 ); var mondeo = new Car( "Ford Mondeo", 2010, 5000 ); //Output( () ); ( () );
In the above example, the toString() method is shared by multiple Car object instances.
For more information about JavaScript, please view the special topic of this site: "JavaScript object-oriented tutorial》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.