The ultimate explanation of JS prototype and prototype chain

Source: Internet
Author: User
Tags tidy

I. Common objects and Function objects
In JavaScript, everything is Object! But the object is also different. is divided into ordinary objects and function objects, object, function is JS comes with the functions of the object. The following examples illustrate

Function F1 () {};
var F2 = function () {};
var F3 = new Function (' str ', ' Console.log (str) ');

var O3 = new F1 ();
var O1 = {};
var O2 =new Object ();

Console.log (typeof Object); function
Console.log (typeof Function); function
Console.log (typeof O1); Object
Console.log (typeof O2); Object
Console.log (typeof O3); Object
Console.log (typeof F1); function
Console.log (typeof F2); function
Console.log (typeof F3); function

In the above example O1 O2 O3 is a normal object, F1 F2 F3 is a function object. How to differentiate, in fact, is very simple, all the objects created by the new function () are function objects, others are ordinary objects. F1,f2, in the final analysis, is created by means of new Function (). Function Object is also created through the New function ().

two. prototype Objects
In JavaScript, each time an object (function) is defined, the object contains some predefined properties. One of the properties of the function object is the prototype object prototype. Note: Normal objects do not have prototype, but have Proto properties.

The prototype object is actually an ordinary object (except for Function.prototype, which is a function object, but it is very special, he has no prototype attribute (previously said the function object has prototype property)). Look at the following example:
Function F1 () {};
Console.log (F1.prototype)//f1{}
Console.log (typeof F1. prototype)//object
Console.log (typeof Function.prototype)/Function, this special
Console.log (typeof Object.prototype)//Object
Console.log (typeof Function.prototype.prototype)//undefined

The output from this console.log (F1.prototype)//f1 {} shows that F1.prototype is an instance object of F1. When F1 is created, it creates an instance object of it and assigns it the prototype, the basic process is as follows:
var temp = new F1 ();
F1. Prototype = temp;

Therefore, function.prototype why the function object is solved, the above mentioned that all the new function () produced by the object is a function object, so Temp1 is a function object.
var temp1 = new Function ();
Function.prototype = Temp1;

What is the prototype object for? The primary function is for inheritance. Give examples:
var person = function (name) {
THIS.name = Name
};
Person.prototype.getName = function () {
return this.name;
}
var zjh = new Person (' Zhangjiahao ');
Zjh.getname (); Zhangjiahao

As can be seen from this example, by setting a property of a function object to Person.prototype, the normal object with the person instance (in the example: ZJH) Inherits this property. Specifically how to achieve the inheritance, it is necessary to talk about the following prototype chain.

three. Prototype chain
JS has a built-in property called Proto when creating an object (whether it is a normal object or a function object), and is used to point to the prototype object prototype of the function object that created it. Take the example above:

Console.log (ZJH. Proto = = = Person.prototype)//true

Similarly, the Person.prototype object has the Proto property, which points to the prototype of the Function object (object) that created it.

Console.log (Person.prototype. Proto = = = Object.prototype)//true

Continue, the Object.prototype object also has the Proto property, but it is more special, NULL

Console.log (Object.prototype. Proto)//null

We strung this proto up until Object.prototype. A chain proto Null is called a prototype chain. Such as:

Four. Memory structure diagram
For a more in-depth and intuitive understanding, let's draw a diagram of the above memory structure:
! [Write a description of the picture here]

Explanation of Doubt:
1.Object. Proto = = = Function.prototype//True
Object is a function object that is created by the new function (), so object. Proto points to function.prototype.

2.Function. Proto = = = Function.prototype//True
function is also an object, and is created by the new function (), so function. Proto points to function.prototype.

Oneself is created by oneself, seem not logical, but think carefully, the real world also have some similar, how you come, your mother is born, your mother how come, your grandmother is born, ... The apes evolved, where the apes came from, all the way back ... that's none, (Null for everything)
As the "moral Sutra" says, "No, the beginning of the world of the name."

3.function.prototype. Proto = = = Object.prototype//true
In fact, I'm a little confused, but I can also try to explain.
Function.prototype is a function object, theoretically his proto should point to Function.prototype, is himself, he points to himself, no meaning.
JS has always emphasized that everything is object, function object is also object, give him to recognize an ancestor, point to Object.prototype. Object.prototype. Proto = = = NULL to ensure that the prototype chain ends normally.

Five. Constructor
The prototype object prototype has a predefined constructor property that references its function object. This is a circular reference
Person.prototype.constructor = = = Person//true
Function.prototype.constructor = = = Function//true
Object.prototype.constructor = = = Object//true

Complete the above memory structure diagram:

There are two points to note:
(1) Note that the object.constructor===function;//true itself is a function-constructed object.
(2) How to find the constructor of an object is to look for the object to which the first constructor property is pointing on the prototype chain of the object

Six. Summary
1. Prototype and prototype chain is a model of JS implementation inheritance.
2. The formation of the prototype chain is really by Proto rather than prototype

To understand this sentence in depth, let's take another example to see if you really understand.
var animal = function () {};
var dog = function () {};

Animal.price = 2000;//
Dog.prototype = animal;
var tidy = new Dog ();

Console.log (Dog.price)//undefined
Console.log (Tidy.price)//2000

Why is it? Draw a Memory diagram:

This shows what the problem is, when executing the dog.price, it is found that there is no price attribute, although prototype points to the animal has this attribute, but it does not go along this "chain" to look for. Similarly, when executing tidy.price, there is no such attribute, but proto points to animal, which will follow this chain to find, animal has the price property, so tidy.price output 2000. As a result, the real formation of the prototype chain is Proro, not prototype.
Therefore, if you specify dog in this way. Proto = animal. That Dog.price = 2000.

The final metaphor, though not very precise, may help with the understanding of the prototype.

Father (Function Object), Sir, a big son (prototype), that is your eldest brother, father to your eldest brother bought a lot of toys, when you were born, the relationship between you (Proto) will let you naturally have your eldest brother's toys. Likewise, you, the older son of yours, buy a lot of toys for him, and when you regenerate your son, your youngest son will naturally have all the toys of your big boy. As to whether they will fight, it is not our business.
Therefore, you are from your eldest brother that inherited, confirmed that the "elder brother like Father" Ah!

JS prototype and prototype chain Ultimate explanation

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.