The constructors in Javascript are also different from other languages. Any function called through the keyword new can be regarded as a constructor.
Inside the constructor body, this points to the newly created object. If there is no return expression displayed in the constructor body, then we will return this by default, that is, the newly created object.
The code copy is as follows:
function Foo() {
this.bla = 1;
}
Foo.prototype.test = function() {
console.log(this.bla);
};
var test = new Foo();
The above code calls Foo as the constructor and points the prototype of the newly created object (__proto__) to Foo.prototype.
If we define the returned expression in the constructor, the constructor will return the entire expression, but this return expression must be an object.
The code copy is as follows:
function Bar() {
return 2;
}
new Bar(); // a new object
function Test() {
this.value = 2;
return {
foo: 1
};
}
new Test(); // the returned object
If new is omitted, the function cannot return a new object.
The code copy is as follows:
function Foo() {
this.bla = 1; // gets set on the global object
}
Foo(); // undefined
The above example may also work in some scenarios, but due to the working mechanism of this in Javascript, this will point to the global object here.
Factory model
In order to be able to use the keyword new, the constructor will have to display a return value.
The code copy is as follows:
function Bar() {
var value = 1;
return {
method: function() {
return value;
}
}
}
Bar.prototype = {
foo: function() {}
};
new Bar();
Bar();
In the above example, the effect of calling the function Bar without using new is the same, and a newly created object containing the method method will be returned, which is actually a closure.
One thing to note here is that new Bar() will not return Bar.prototype, but will instead be a prototype object of the function method inside the return expression.
In the above example, there is no difference in functionality between using new or not.
Create new objects through factory mode
We are often reminded not to use new because once you forget about its use it will cause errors.
To create an object, we prefer to use factory pattern and construct a new object within factory pattern.
The code copy is as follows:
function Foo() {
var obj = {};
obj.value = 'blub';
var private = 2;
obj.someMethod = function(value) {
this.value = value;
}
obj.getPrivate = function() {
return private;
}
return obj;
}
Although the above example code is less prone to errors than when using new and will be more convenient when using private variables, there are some disadvantages:
Because prototype objects cannot be shared, more memory is required.
To achieve inheritance, the factory pattern requires copying all methods of another object or using it as a prototype of a new object.
Giving up the prototype chain is just to avoid using new, which seems to be contrary to the spirit of the Javascript language.
Summarize
Although using new may be more prone to errors, this is not a reason to abandon the use of prototype chains. As for which method to take in the end, it depends on the needs of the application. The best way is to choose a style and stick to it.
Simply put, the constructor is to initialize an instance object, and the object's prototype property inherits an instance object.