The new operator can be used in Javascript to create an object. How does the system use ne?
In Javascript, the new operator can be used to create an object. How does the system use the new operator to create an object? Let's take a look at the process:
- First, define a class.
- Use the new operator to follow the function you defined to create a new class instance.
- Once the Javascript compiler encounters the new operator, it creates an empty instance variable and copies all prototype attributes and methods in the class to this instance, and point all this in the member function to the newly created instance.
- Next, execute the function that follows the new operator.
- When you execute this function, if you try to assign a value to a non-existent attribute, the Javascript compiler will automatically create this attribute for you within this range.
- After the function is executed, the initialized instance is returned.
That is to say:
JavaScript actually performs the following three processes when using new:
- Create an empty object.
- Copy the attributes and methods in the prototype of the class to the instance.
- Use the null object created in step 1 as the class parameter to call the class constructor.
Simulate new
Description: Creates a class person, creates two variables p1 and p, and compares the final results of the two variables.