The relationship between __proto__ and prototype in JavaScript and the principle of prototype inheritance

Source: Internet
Author: User

The relationship between 1.__PROTO__ and prototype

The __proto__ of all objects point to the prototype of their constructors

var a = {"Test": ' Mico ', "Age": "};var" B = function () {};var c = new Date (); var d =/^[\d]$/;alert (a.__proto__ = object.pr Ototype); Truealert (b.__proto__ = = Function.prototype); Truealert (c.__proto__ = = Date.prototype); Truealert (d.__proto__ = = Regexp.prototype); True

  

Let's take a look at this piece of code

The printed t is like this, so what __proto__ is. Each object initializes a property inside it, that is, __proto__, when we access the property of an object, if the object does not exist inside this property, then he will go to __proto__ to find this attribute, this __proto__ will have their own __proto__, So we have been looking for, that is, what we usually call the concept of the prototype chain, when the object found in the constructor of this property has not yet, it will return to undefined.

The prototype chain inherits var Base = function () {};  Base.prototype.say = function () {alert ("saying");}; Base.prototype.getSaraly = function () {alert ("5000");}; Base.prototype.name = "Mico"; var Ctrl = function () {}; Ctrl.prototype = new Base (); Ctrl.prototype.name = ' perfunctory ah ';         var a = new Ctrl (); Console.log (a); alert (a.name); Perfunctory Ah A.say (); Saying                alert (a.__proto__ = = Ctrl.prototype);//true                alert (a.__proto__.__proto__ = Base.prototype);//true

  

The print out of a is like this

As can be seen, when printing a.name, directly in the __proto__ inside find, found the direct return,

When printing a.saying, it was not found in __proto__. Will go down a layer __proto__ inside find, found the direct return, if find the constructor of __proto__ not found, return undefined.

Print

Alert (a.__proto__ = = Ctrl.prototype);//true
Alert (a.__proto__.__proto__ = = Base.prototype);//true

This is the addition of the prototype chain, __PROTO__ is a private property, and prototype is provided as an external interface, you can change the __proto__ property by prototype.

The relationship between __proto__ and prototype in JavaScript and the principle of prototype inheritance

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.