Hello everyone, through some examples today, I will introduce the function of new at the bottom of Javascript.
Calling the constructor directly without the function of new, it can be seen that "undefined" is returned, because the constructor has no return value without the function of new.
By printing this, you can see that this inside the constructor points to window, which contains various object parameters.
By adding new, there is a returned result.
The role of new actually creates a new empty object this = {}, which is implicit. Point the this of the constructor to the object to be returned.
The following briefly summarizes the process of new:
1. Create a new empty object
2. Assign the scope of the constructor to the new object (this points to the new object)
3. Execute the constructor code (add properties to this new object)
4. Return the new object