On JS prototype and prototype chain

Source: Internet
Author: User

Before we discuss prototypes, we need to know what prototypes are, and keep these rules in mind .

  A prototype is a default property (prototype) of all function (constructor) objects, and its value is an object {what is inside}; So we generally say that the prototype is the property of the constructor, and it is also an object;

What the prototype does, the properties and methods inside the prototype, can be inherited by the object created by the constructor (this function can reduce the coupling of the Code);

function Fun () {

Name: "Zhang San"

}

At this point, the constructor fun has a prototype (default property prototype), its value is a {}, and now we add a property method to its prototype

Fun.prototype.a = "QQ";

fun.prototype.b= function () {

return 123;

}

Now, we use the constructor fun to create (new) an object;

var fun = new Fun ();

So according to our understanding, the fun object inherits the methods and properties of the constructor fun prototype;

Console.log (FUN.A) output QQ

Console.log (fun.b) output 123

At this point we can say that as long as the object created by the fun constructor, there are both A and B properties (methods).

We now know what a prototype is, but how can we see the prototype? The __proto__ property needs to be used here.

The __proto__ property (view prototype) is the default property of the object (note not all), and its value is a prototype of the constructor that created the object (prototype)

Note: The value of __proto__ cannot be changed by the __proto__ property, only viewable, if needed to change the need to use prototype;

So let's look at the next fun prototype:

FUN.__PROTO__ ==> Create a prototype of a fun object's constructor ==> Fun.prototype

So,fun.__proto__ = Fun.prototype , funinherits the properties of its prototype (Fun.prototype)

Let's look at the prototype of the fun prototype.

FUN.__PROTO__.__PROTO__, namely fun.prototype.__proto__

Note: Fun.prototype is an object at this point because, as mentioned earlier, the value of the prototype is an object (typically object, with some exceptions)

According to the inference just now:

FUN.PROTOTYPE.__PROTO__ ==> Create a prototype of the Fun.prototype object's constructor ==>object.prototype

fun.prototype.__proto__ = Object.prototype at the same time Fun.prototype inherits the properties of its prototype (Object.prototype)

At this point, Object.prototype is the final prototype, because all objects are created by the object constructor (which is itself), and the value of object.__prototype__ is null; it has reached the end, that is fun.__proto__ . __proto__.__proto__ = null

fun.__proto__

fun.__proto__.__proto__

fun.__proto__.__proto__.__proto__

Through the __proto__ property, now has a chain, Object Lookup Property (method), first in their own inside, no then find its prototype, or not, and then find its prototype prototype, has been found in the last, or did not return undefined; this is the so-called prototype chain

We're looking at the prototype of fun,

Because fun is a constructor object, and constructors are created by function,

fun.__proto__ = Function.prototype; Be sure to remember that the prototype is an object, that is, Function.prototype is an object.

So the prototype of the fun prototype is

fun.__proto__.__proto__ = function.prototype.__proto__

The prototype of the Function.prototype object is Object.prototype

Function.prototype.__proto__=object.prototype

According to this rule, Object.prototype is the final prototype of all objects.

Learn the benefits of prototyping and prototyping chains

A new understanding of the properties and methods of the objects, the structure is clearer, so I can write a lot less repetitive code

fun.__proto__ = Fun.prototype

object. __proto__ = constructor that creates the object. prototype

This equation needs to be kept in mind

There are some personal understanding of the prototype and the prototype chain, the wrong place to look at a lot of evidence

On JS prototype and prototype chain

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.