JavaScript Object System in-depth analysis 3-Create Object. Create

Source: Internet
Author: User

3. Create Object. Create

 

@ Wu Qiong Adam

 

The feedback from the students over the past two days is hard to understand. Therefore, I decided to give another example to help you understand it. The wonderful content about Scope and Closure will have to be written later!

 

In fact, this series is not intended for beginners, but for intermediate JavaScript programmers who have a certain understanding of JS. I hope this series can help them see the essence through phenomena. At The same time, The content here is highly concentrated, which basically corresponds to The content of JavaScript: The Good Parts Chapter 2. Therefore, it is recommended that you are interested in, it is best to use the preceding example program and Debugger to understand the Object System of JS. it is not practical to expect that the object system of JS will be fully understood by reading this blog series.

 

In addition, if you simply write a webpage, you do not need to understand JS clearly. However, if you really pay attention to JS, you are planning to apply such as node. js, backbone. for js frameworks like JS, I think we still need to really understand JS.

 

Before reading the following content, read 1, 2:

1. Object: http://www.bkjia.com/kf/201201/117897.html

2. function object: http://www.bkjia.com/kf/201201/117898.html

Let's get started. In JavaScript: The Good Parts, we provide a common method for creating objects:

Object. create = function (o ){

Var F = function (){};

F. prototype = o;

Return new F ();

};

Var B = Object. create ();

Based on the content in Section 1 and 2, let's take a look at the above Code:

 

First, we have an Object a before executing var B = Object. create (a);, for example:

 

After var F = function () {}; is executed, the F function object is created, and Its property is assigned to a newly created object, detailed Rules are described in section 2:

When a function is defined using var ff = Function () {}, JS generates a function object F, and the _ proto _ pointer of Function object F points to function. prototype (Omitted in), and F also has a property. The property value points to a newly created object P. This object P has only one property, constructor (), the constructor () value of the constructor is the function Object F, while the _ proto _ pointer of the Object P points to the Object. prototype,

 

 

After F. prototype = o; is executed, the prototype attribute of F points to object A, as shown in:

 

After new F () is executed, according to the rules described in section 2nd, the object relationships after execution are as follows:

When a function is activated with new, a new object is created, and the content of the object is the result returned by the execution function, the _ proto _ link of this object points to the object referenced by the prototype attribute of the function.

 

After the function Object. create returns, F and p do not exist. Therefore, the final Object relationship is as follows:

 

To sum up, we can see that the _ proto _ link of the Object cannot be directly modified, but the prototype attribute of the function Object can be modified. Therefore, the Object. create actually utilizes this feature and completes a copy creation process with the help of new. I hope this example can help you better understand it !!

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.