Understanding the implementation principle of Javascript_11_constructor _ javascript skills

Source: Internet
Author: User
After understanding the object model, we can see how the constructor attribute is implemented. What is constructor?

In a simple understanding, constructor refers to the object constructor. See the following example:

The Code is as follows:


Function Foo (){};
Var foo = new Foo ();
Alert (foo. constructor); // Foo
Alert (Foo. constructor); // Function
Alert (Object. constructor); // Function
Alert (Function. constructor); // Function


For foo. constructor as Foo, I think it should be well understood because foo's constructor is Foo. I think there is no dispute over the Function constructors of Foo, Object, and Function. (Because Foo, Object, and Function are both Function objects, and because all Function objects are constructed from Function objects, their constructor is Function, for details, see JavaScript _ function objects.)

Relationship between Prototype and Constructor

The Code is as follows:


Function Dog (){}
Alert (Dog === Dog. prototype. constructor); // true


In JavaScript, each function has a property named "prototype" and is used to reference a prototype object. This prototype object is also known as the "constructor" attribute, which references the function itself in turn. This is a type of circular reference,

Where does the constructor attribute come from?
Let's take a look at the construction process of Function String construction:

Note: The process of constructing Function objects is the same. Therefore, the construction process is the same for both built-in objects such as String, Boolean, Number, and user-defined objects. Here, String is just a representation!
As shown in the figure, constructor is generated when a Function object is created by a Function. As described in 'relationship between prototype and constructor ', constructor is an attribute in the prototype chain of the Function object. That is, String = String. prototype. constructor.

I also want to use a piece of code to prove that the theory is correct:

The Code is as follows:


Function Person (){}
Var p = new Person ();
Alert (p. constructor); // Person
Alert (Person. prototype. constructor); // Person
Alert (Person. prototype. hasOwnProperty ('constructor'); // true
Alert (Person. prototype. isPrototypeOf (p); // true
Alert (Object. prototype. isPrototypeOf (p); // true
Alert (Person. prototype = Object. prototype); // false


Now, you will find that this is in conflict with the default prototype pointing to Object. prototype in the previous prototype implementation principles. Obviously, the theory at that time was not very comprehensive.

Special Object
Attentive readers may ask such a question. Your theory does not apply to objects. Because the following code conflicts with your above theory:

The Code is as follows:


Alert (Object. prototype. hasOwnProperty ('constructor'); // true
Alert (Object. prototype. hasOwnProperty ('isprototypeof '); // true. If it is based on the above theory, false should be returned here.


Is that true? No! Let's take a look at how special objects are handled:

You will find that the principle of this image is the same as that of the above image. This correctly explains that Object. prototype. hasOwnProperty ('isprototypeof ') is true!

Constructor

The Code is as follows:


Function Animal (){}
Function Person (){}
Var person = new Person ();
Alert (person. constructor); // Person


Based on the content in the previous section, can you correctly understand the results of this code? After thinking about it, let's take a look at its memory representation:

This figure clearly shows how the Function constructs Animal and Person. The relationship between person and Person is also displayed.

The Code is as follows:

The Code is as follows:


Function Animal (){}
Function Person (){}
Person. prototype = new Animal ();
Var person = new Person ();
Alert (person. constructor); // Animal


At this time, the person constructor is Animal. How can this problem be solved?

Note: The dotted line in the figure indicates the default prototype pointing to Person (for reference only ). However, we direct Person. prototype to new Animal.
In this case, the prototype of Person points to the Animal instance, so the constructor of person is the Animal constructor.

Conclusion: The constructor principle is very simple. It is to find the constructor attribute on the prototype chain of the object.

Note: if you cannot correctly understand the content of this article, review the content in the previous chapter.

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.