Prototypes and prototype chains in JavaScript

Source: Internet
Author: User

JavaScript is an object-oriented language, but it does not have a class concept like other object-oriented languages (such as java,c++), and there is no class-based inheritance system. But it has its own unique way of inheriting, which is based on the inheritance of prototypes and prototype chains.

When we create an object, each object has an implicit property __proto__ after the build (not a specification, for the time being), which points to the prototype object of the instance and shares the properties and methods on its prototype object.

The following analysis differs from the __proto__ attribute that corresponds to how different objects are created.

1. Create objects with object literals.

var a={x:1};console.log (a.__proto__); // {}

The above code indicates that the __proto__ property of the A object is an empty object, does it mean that the object created with the object literal begins with the __proto__ property that is an empty object? Let's look at the code below.

var a={x:1};object.prototype.y=1; Console.log (a.__proto__); // {y:1}Console.log (A.Y); // 1

It is obvious from the above code that the __proto__ of a is pointing to the prototype object, and a inherits the attributes on the object prototype. So the __proto__ property of an object created with the object literal is pointing to the prototype object.

2. Create an object using the constructor method.

function Foo (name) {this. Name = name;} b=new foo ("B"); foo.prototype.sayName=function() {Console.log (this  . name);} Console.log (b.__proto__===foo.prototype); // trueCosnsole.log (B.sayname ()); // b

The __proto__ of object B created by the constructor points to Foo.prototype.

The above analysis of different ways to create the object of the different prototype, then say the prototype chain

From the above analysis we know that each object has a __proto__ attribute pointing to its prototype object, and the prototype object itself is an object, and it also has a __proto__ attribute pointing to its prototype object, so repeatedly, to form a chain structure. We have learned the chain structure of linked lists, which must have an end point, or it will form a dead chain. Where is the end of the prototype chain?

 function   Foo (name) { this . Name = name; b  =new  foo ("B"  =function   () {Console.log ( this  .name);} Console.log (foo.prototype.__proto__  ===object.prototype) // true  Console.log (foo.prototype.__proto__.__proto__) // null  Console.log (foo.prototype.__proto__.__proto__.__proto__) // Span style= "color: #008000;" > error, null no __proto__ property  

The __proto__ of the Foo.prototype object points to Object.prototype, and the Object.prototype.__proto__ property value is null and no longer has a __proto__ attribute, which means that the prototype chain terminates.

In this article to record their own prototype and prototype chain of understanding, welcome criticism.

Prototypes and prototype chains in 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.