The prototype object of JavaScript and the _javascript technique of prototype chain

Source: Internet
Author: User

For the new, JavaScript prototype is a very annoying thing, one prototype easy to confuse with __proto__, and the various points between them is really a bit complicated, in fact, there have been a lot of articles on the market to try to say clearly, there is a so-called very classic diagram, The above painted a variety of lines, one will connect this one will connect that, to tell the truth I see very dizzy, not to mention fully understand. So I want to try it myself and see if I can break out the important knowledge points in the prototype and make it clear in the simplest chart form.

We know that the prototype is an object that other objects can implement for inheritance of attributes. But the NI besides prototype, have a __proto__ is used for why? How does it make a difference if you look like that? They all point to who, so chaos how to remember AH? What the hell is a prototype chain? Believe that many beginners even have some experience of the veteran are not necessarily completely clear, the following with three simple diagram, with some sample code to understand.

I. The difference between prototype and __proto__

var a = {};
Console.log (A.prototype); Undefined
console.log (a.__proto__);//object {}

var b = function () {}
Console.log (b.prototype);//b {}
Console.log (b.__proto__);//function () {}

/*1, literal means * *
var a = {};
Console.log (a.__proto__); Object {}

console.log (a.__proto__ = = A.constructor.prototype);//true

/*2, constructor mode/
var a = function () {} ;
var a = new A ();
Console.log (a.__proto__); A {}

console.log (a.__proto__ = = A.constructor.prototype);//true

/*3, object.create () way * *
var a1 = {A : 1}
var a2 = object.create (A1);
Console.log (a2.__proto__); Object {a:1}

console.log (a.__proto__ = = A.constructor.prototype);//false (here is the exception in Figure 1)

var A = function () {};
var a = new A ();
Console.log (a.__proto__); A {} (that is, the prototype object of constructor function A)
console.log (a.__proto__.__proto__);//object {} (that is, the prototype object of constructor function object)
Console.log (a.__proto__.__proto__.__proto__); Null

The above is the entire content of this article, this article uses three diagrams to explain the JavaScript prototype object and the prototype chain, the hope is helpful to everybody learning JavaScript.

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.