The role of new at the bottom of Javascript

   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.



insert image description here



   By printing this, you can see that this inside the constructor points to window, which contains various object parameters.



insert image description here



   By adding new, there is a returned result.
insert image description here



   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.



insert image description here



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

Supongo que te gusta

Origin blog.csdn.net/weixin_48591974/article/details/129322088
Recomendado
Clasificación