Javascript constructor is very powerful and may be one of the features that Javascript can fully utilize. But if you want to really understand Javascript, you should still understand how constructors work. This article describes constructor in three aspects.
1. What is a constructor?
Constructors are common in some object-oriented languages, such as Java, C ++, and PHP. In Javascript, constructor is a common function that can be called using the new operator and generate a special type of object.
The Code is as follows:
// "Benjamin" is a constructor
Var benjamin = new Benjamin ("zuojj", "male ");
In the above example, benjamin is a Benjamin object. How is it instantiated?
The Code is as follows:
Function Benjamin (username, sex ){
This. username = username;
This. sex = sex;
}
Var benjamin = new Benjamin ("zuojj", "male ");
// Outputs: Benjamin {sex: "male", username: "zuojj "}
Console. log (benjamin );
As we can see, the "Benjamin" constructor only receives passed parameters and assigns them to this object. This is because when the constructor is called by the new operator, this object of the constructor is assigned a value to the object returned by the new operation.
This means that the above Code is equivalent:
The Code is as follows:
Benjamin = {
"Username": "zuojj ",
"Sex": "male"
}
Ii. Why are constructors used?
There are several reasons for using constructors:
1. Using constructors means that all these objects can be created using the same basic structure.
2. Using constructors means that the "benjamin" object is explicitly labeled as an instance of the "Benjamin" function
The Code is as follows:
Function Benjamin (username, sex ){
This. username = username;
This. sex = sex;
}
Var benjamin = new Benjamin ("zuojj", "male ");
Var ben = {
"Username": "zuojj ",
"Sex": "male"
}
// Outputs: true
Console. log (benjamin instanceof Benjamin );
// Outputs: false
Console. log (ben instanceof Benjamin );
3. Using constructors means that we can define public methods on the prototype for multiple instances to share.
The Code is as follows:
Function Benjamin (username, sex ){
This. username = username;
This. sex = sex;
}
Benjamin. prototype. getName = function (){
Return this. username;
}
Var benjamin = new Benjamin ("zuojj", "male ");
Var ben = new Benjamin ("lemon", "female ");
// Outputs: zuojj
Console. log (benjamin. getName ());
// Outputs: lemon
Console. log (ben. getName ());
Iii. Notes
1. new Keyword
Do not forget to use the new keyword when instantiating the constructor. whether to use the new keyword has a great impact on this object, this object will point to the global Object (window in browser and global in node ). Therefore, when defining a constructor, we recommend that you use uppercase letters for the function name.
2. if the called function does not have an explicit return expression, this object-the newly created object will be returned implicitly. Otherwise, the returned results will be affected, but only one object will be returned.
The Code is as follows:
Function Bar (){
Return 2;
}
Var bar = new Bar ();
// Return the newly created object
// Outputs: Bar {}
Console. log (bar );
Function Test (){
This. value = 2;
Return {
Foo: 1
};
}
Var test = new Test ();
// Returned object
// Outputs: Object {foo: 1}
Console. log (test );
Note the following:
A) The new Bar () returns the newly created object, not the literal value 2 of the number. Therefore, new Bar (). constructor = Bar, but if a numeric object is returned, the result is different;
B) The new Test () obtained here is the object returned by the function, rather than the newly created object through the new Keyword, as shown below:
The Code is as follows:
Function Bar (){
Return 2;
}
Var bar = new Bar ();
Function BarN (){
Return new Number (2 );
}
Var barn = new BarN ();
// Outputs: true
Console. log (bar. constructor === Bar );
// Outputs: Number {}
Console. log (barn );
// Ouputs: false
Console. log (barn. constructor === BarN );
// Outputs: true
Console. log (barn. constructor === Number );
/*--------------------------------------*/
Function Test (){
This. value = 2;
Return {
Foo: 1
};
}
Var test = new Test ();
// Outputs: undefined
Console. log (test. value );
// Ouputs: 1
Console. log (test. foo );
The above is a summary of the constructor. I hope it will be helpful to beginners. I hope to criticize and make an axe to the question.