JavaScript prototypes and prototype chains (notes)

Source: Internet
Author: User

No matter any article, do not understand, two times, two times do not understand, continue to see, technology is constantly upgrading, until one day you suddenly found that it is so clear ...

Prototype this piece has always wanted to summarize, the value to today only then the pen

Prototype

Defined:

JS all functions have a prototype attribute, which refers to an object, the prototype object, or the prototype. we are talking more about the prototype of the constructor, but we cannot deny that the common function also has a prototype.

Everything in JavaScript is object, each object has a __proto__ property, and the function's __proto__ corresponds to "function () {}".

The function also has the prototype property.

The prototype of an instance of the __proto__=== constructor.

Role:

Prototypes for adding properties and methods to objects

The relationship between constructor prototypes, and so on:

function P () {};

var p=new p ();

When a constructor p is created, it has a prototype property, and when new is an instance p, the __proto__ property of the example p corresponds to the prototype property of the constructor p; (p.prototype===p.__proto__)

The __proto__.constructor of the instance p corresponds to the constructor p; (P.__proto__.constructor===p () {})

thereby p.__proto__.constructor.prototype points to P.prototype (P.__proto__.constructor.prototype===p.prototype)

P.constructor===p.__proto__.constructor

P.constructor.prototype with the internal _proto_ is not the same thing, instantiate the object with _proto_, instance p is not prototype attribute, but there is internal __proto__, through __proto__ To get prototype properties and prototype methods on the prototype chain.

Prototype chain

Defined:

Everything in JavaScript is object, each object has a __proto__ property, and the __proto__ property points to its constructor prototype that instantiates it .... So it's going to form a __proto__ chain.

Prototype chain effect:

Implementing inheritance of object properties and methods

ES6 class can be inherited through the extends keyword, more convenient

If there is a wrong place, please advise! --rlflash

JavaScript prototypes and prototype chains (notes)

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.