1. What is a constructor
Constructors are very common in some object-oriented languages such as Java, C++, and PHP. In Javascript, the constructor is first a normal function, which can be called using the new operator and generates an object of special type.
The code copy is as follows:
// "Benjamin" is a constructor
var benjamin = new Benjamin("zuojj", "male");
In the above example, benjamin is a Benjamin object, so how does it instantiate?
The code copy 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 simply receives passed parameters and assigns them to this object. This is because when the constructor is called by the new operator, the constructor's this object is assigned to the object returned by the new operation.
This means that the above code is equivalent to:
The code copy is as follows:
benjamin = {
"username": "zuojj",
"sex": "male"
}
2. Why use constructors
There are several reasons why constructors are used:
1. Using constructors means that all these objects can be created using the same basic structure.
2. Using a constructor means that the "benjamin" object is explicitly marked as an instance of the "Benjamin" function
The code copy 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 sharing multiple instances
The code copy 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());
3. Things to note
1.new keywords
When instantiating the constructor, you must not forget to use the new keyword. Whether to use the new keyword will have a great impact on this object. Without the new keyword, this object will point to the global object (window in browser and global in node). Therefore, when defining a constructor, it is recommended that the first letter of the function name is capitalized.
2. If the called function does not have an explicit return expression, this object will be returned implicitly, which is the newly created object. Otherwise, the result will be affected, but only if the returned object is
The code copy 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);
What we need to pay attention to is:
a) new Bar() returns the newly created object, not the literal value of the number 2. Therefore new Bar().constructor === Bar, but if the returned number object, the result will be different;
b) The new Test() obtained here is an object returned by the function, not a newly created object through the new keyword, as shown below:
The code copy 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. If there are any inappropriate points in the article, I hope to criticize and correct them.