JavaScript prototype chain inheritance

Source: Internet
Author: User

There is no concept of a class in JavaScript, there is only one constructor to create the object.

But JavaScript can also implement inheritance.

The first thing to say is that the objects in JavaScript are divided into function objects and normal objects.

What is a function object??

This is the object.

function f ()

{

}

In addition to the function object, all other objects are normal objects.

Let's talk about the relationship between constructors, instances, and prototype objects.

A prototype property inside a function object is used to refer to the object's prototype object. In fact, the prototype object is also very common.

The equivalent of implicitly writing code like this.

var fprototype = new F ();

F.prototype = Fprototype;

There is not much difference between a prototype object and a generic instance.

In general, the instance generated by the constructor has such a property---> __proto__ This property refers to its own prototype object.

Use a picture to illustrate the relationship between the three.


Next we're going to look at a break code.



Here, the author clearly does not define the Username property in the constructor but the result is the value.

Here it is, now look inside the current object to see if it has the current property name, if not the prototype object to find the attribute. Always follow the prototype chain to find Object.prototype.

If it is not found, it is undefined.

By applying this principle we can implement JavaScript inheritance.

We can draw a rough picture of this inheritance process.



Therefore, the key to implement the prototype chain inheritance is __proto__ rather than the prototype attribute in the function object.

Demo of code on prototype inheritance

function Parent (username)
{
    this.username = "AAA";
}
Parent.prototype.sayHello = function ()
{
    console.log (this.username);
};

PARENT.PROTOTYPE.AAA = function ()
{
    console.log ("HelloWorld");
function child (username, password)
{
    //Parent.call (this, username);
    This.password = password;
}

Child.prototype.sayWorld = function ()
{
    console.log (This.password);
};


Child.prototype = new Parent ();

var child = new Child ("Zhangsan", "123");
Child.sayhello ();
Console.log (child.__proto__.__proto__.__proto__.__proto__);

If there is a wrong place to speak, welcome to treatise, and hope to help you readers.

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.