JavaScript Advanced Object-oriented (13)--The execution procedure of the constructor function

Source: Internet
Author: User

Tag: It's called ASC initialization operator ext Process Object-oriented what

Description (2017-4-2-21:50:45)

What is the function of the constructor:

1. Initialize the data.

2. To add attributes to the object in JS, initialize the attribute value with.

Second, the process of creating objects:

1. Code: var p = new person ();

2. First operator new creates an object that resembles {} and is a "no member" object.

* Use new to create an object whose type is the name of the constructor that created it (for example, person type).

* Use {} Anyway is the object type, which is equivalent to "new object ()".

3. Then call the constructor to initialize the member.

* constructor at the beginning of the call, there is an assignment operation, the object that this= just created.

* Therefore in the constructor, this represents the object that was just created.

4. In the constructor, add members to the object using the object's dynamic properties.

Third, analyze the code, write out the implementation process of each step:

Break point in line 2nd, but execution starts directly from line 7th, because 7 rows are assignments, and 2 lines are just function declarations. Run-by-statement, you can see in watch that this is slowly increasing the property, and finally P has three properties.

1 <script type= "Text/javascript" >2     function person (name, age, gender) {  3this         . name = name; 4         this. Age = Age ; 5         this. gender = gender; 6     }7     varnew person ("Zhang San", 19, "male"); 8 </script>

JavaScript Advanced Object-oriented (13)--Constructor execution procedure

Related Article

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.