Constructor, operator new
Javascript Constructor, operator “new” (Javascript构造函数,运算符“new”)
The “new” operator allows creating an instance of a user-defined object type or a built-in operator type that has a constructor function. You can do the following things using the “new” keyword. (“new”运算符允许创建用户定义的对象类型或具有构造函数的内置运算符类型的实例。您可以使用“new”关键字执行以下操作。)
You can create a blank object of JavaScript;
Link this object to another one;
Pass the freshly created object from Step 1 like this context;
Get back this in case the function doesn’t return its object. (-获取this ,以防函数不返回其对象。)
The {…} syntax lets developers create a single object. But, sometimes, it is necessary to generate many similar objects such as menu items, multiple users, and more. You can do it using constructor functions and with the help of the “new” operator.
Two conventions exist, though:
You should name them with the capital letter first;
You must run them only using the “new” operator (-您必须仅使用“new”运算符运行它们)
Here is an example:
function Car(name) {
this.name = name;
this.isSlow = false;
}
let car = new Car("BMW");
console.log(car.name); // BMW
console.log(car.isSlow); // false
Whenever you invoke a function with “new”, the following actions are taken:
Creating a new empty object and assigning to this. (-创建一个新的空对象并分配给此对象。)
Executing the function body. As a rule, it modifies this adding new properties to it. (-执行函数体。通常,它会修改此添加的新属性。)
Returning the value of this. (-返回此值的值。)
So, new Car(…) acts like this:
function Car(name) {
// this = {}; (implicitly)
// add properties to this
(//向此添加属性)
this.name = name;
this.isSlow = false;
// return this; (implicitly)
}
The result of let car = new Car(“BMW”) will be the same as:
let car = {
name: "BMW",
isSlow: false
};
Now, if you wish to create new cars, you can call new Car(“BMW”), new Car(“Mercedes”), etc. (现在,如果您想创建新车,您可以调用新车( “宝马” ) ,新车( “梅赛德斯” )等。)
It’s both shorter than using literals and much easier to read. (它既比使用文字短,又更易于阅读。)
The aim of the constructors is to implement a reusable object creation code. (构造函数的目的是实现可重用的对象创建代码。)
In the event of having different lines of code all about creating one complex object, wrap them in the constructor function, as demonstrated in the following example:
new function() { … }
new function() { … }
In the event of having different lines of code all about creating one complex object, wrap them in the constructor function, as demonstrated in the following example:
let car = new function () {
this.name = "BMW";
this.isSlow = false;
//another code for creating a car
(//创建汽车的另一个代码)
//can be complex logic and statements
(//可以是复杂的逻辑和语句)
//local variables etc
};
Keep in mind that the constructor can’t be called once more, as it is not saved. It is just created and saved. (请记住,无法再次调用构造函数,因为它未保存。它刚刚创建并保存。)
Return from Constructors
Return from Constructors (从构造函数返回)
In general, a return statement is not used for constructors. Their primary purpose is writing the overall necessary content into this, and it mechanically becomes turns into the result. (一般来说, return语句不用于构造函数。他们的主要目的是将整体必要的内容写入其中,并机械地转化为结果。)
But if a return statement exists, there is a simple rule. It is the following:
In case you call return with an object, the latter is returned instead of this. (-如果您使用对象调用return ,则返回后者而不是this。)
In case you call return with a primitive, it will be ignored. (-如果您使用原语调用return ,它将被忽略。)
In the following example, return overrides this:
function Car() {
this.name = "BMW";
return {
name: "Mercedes" // returns this object
};
}
console.log(new Car().name); // Mercedes, got that object
And here is another example with an empty return:
function Car() {
this.name = "BMW";
return; //returns this
}
console.log(new Car().name); // BMW
Omitting Parentheses
Omitting Parentheses (省略括号)
Anyway, it is possible to omit parentheses after the new, if it doesn’t have arguments:
let car = new Car; //no parentheses
// same as
let car = new Car();
Constructor Methods
Constructor Methods (构造函数方法)
It is convenient to use constructor functions for creating objects. The constructor functions may include parameters defining how to construct the particular object, and what to put inside of it. (使用构造函数创建对象非常方便。构造函数可以包括定义如何构造特定对象以及在其中放置什么的参数。)
Moreover, it is possible to add to this not only properties but also methods. (此外,不仅可以添加属性,还可以添加方法。)
For example, a new Car(name) will create an object with a particular name and the sayCarName method:
function Car(name) {
this.name = name;
this.sayCarName = function () {
console.log("Car name is: " + this.name);
};
}
let bmw = new Car("BMW");
bmw.sayCarName(); // Car name is: BMW
/*
bmw = {
name: "BMW",
sayCarName: function() { ... }
}
*/
Summary
Summary (概要)
Constructors are regular functions, but it is preferred to name them with the capital letter first. They should merely be called with new. A call like this will create an empty this at the beginning and return the populated one at the end. (构造函数是常规函数,但最好先用大写字母命名。他们应该仅仅被称为新的。这样的调用将在开头创建一个空的this ,并在结尾返回填充的this。)
Constructors are used for creating multiple similar objects as well. (构造函数也用于创建多个类似的对象。)