The process of creating an object in js

Source: Internet
Author: User
This article mainly introduces the process of a new object in js. It has good reference value. Let's take a look at it below. This article mainly introduces the process of a new object in js. It has good reference value. Let's take a look at it together with the small Editor.

Use the new Keyword to call a function (new ClassA (...)) Specific steps:

1. Create an empty object {}

2. Use the new object to call the function. this in the function is directed to the new instance object:

{}. Constructor ();

3. Set the constructor attribute of the new object to the name of the constructor, and set the _ proto _ attribute of the new object to point to the prototype object of the constructor.

4. Save the initialized object address to the variable on the left of the equal sign.

NOTE: If no return value is returned in the constructor or the return value is of the basic type (Number, String, Boolean), the new instance object is returned. If the return value is of the reference type, the actual return value is of this reference type.

Var foo = "bar"; function test () {this. foo = "foo";} new test (); // this in test indicates the new object and does not change the global foo Attribute console. log (this. foo); // "bar" console. log (new testThis (). foo); // "foo"; new and attribute access. the operator has the same priority and runs from left to right.

All of the above are personal understandings. If you have any mistakes, please leave a message to correct them.

For more articles about the process of adding a new object in js, please follow the PHP Chinese network!

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.