How the JavaScript interpreter creates an object

Source: Internet
Author: User
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:

  1. First, define a class.
  2. Use the new operator to follow the function you defined to create a new class instance.
  3. 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.
  4. Next, execute the function that follows the new operator.
  5. 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.
  6. After the function is executed, the initialized instance is returned.

That is to say:

JavaScript actually performs the following three processes when using new:

  1. Create an empty object.
  2. Copy the attributes and methods in the prototype of the class to the instance.
  3. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.