Understanding of _ PROTO _ and prototype

Source: Internet
Author: User

From: http://www.cnblogs.com/zzcflying/archive/2012/07/20/2601112.html


var
Person = function (name) {      this .name = name; }
var
p = new
Person();

newOperator operation isvar
p = {};p.__proto__ =  Person.prototype;Person.call(p);

VaR P = {}; that is, initialize an object p.

P. _ PROTO _ = person. Prototype;

Person. Call (P );

That is to say, Constructing P can also be called initializing p.

The key lies in the second step. Let's prove it:

var
Person = function() {}var
p = new
Person();alert(p.__proto__ = = Person.prototype)

This code returns true. It indicates that step 2 is correct.

So what is _ PROTO? Let's briefly describe it here. Each object will Initialize an attribute inside it, namely _ PROTO __. when we access an object's attribute, if this attribute does not exist inside the object, then he will go to _ PROTO _ to find this attribute, and this _ PROTO _ will have his own _ PROTO __, so he will keep searching like this, that is, the concept of prototype chain.

According to the standard, __proto _ is not made public, that is, it is a private property, but the Firefox and chrome engines expose it to a common property, we can access and set it externally.

The concept is clear. Let's look at the following code:

var
Person = function
() { };Person.prototype.Say =
function
() {
    alert("Person say");}var
p = new
Person();p.Say(); 

This code is very simple. I believe everyone has written it like this. Let's see why P can access the person's say.

First var P = new person ();

We can get p. _ PROTO __= person. prototype. So when we call p. when you say (), P does not have the say attribute. Therefore, he needs to find it in _ PROTO _, that is, person. prototype, and the person. prototype. say = function () {}; then, this method is found.

Okay. Next, let's look at a more complex one.

var
Person = function
() { };Person.prototype.Say =
function
() {
    alert("Person say");}Person.prototype.Salary = 50000;

var
Programmer = function
() { };Programmer.prototype =
new
Person();
Programmer.prototype.WriteCode =
function
() {
    alert("programmer writes code");};Programmer.prototype.Salary = 500;

var
p = new
Programmer();p.Say();p.WriteCode();alert(p.Salary);  

Let's make the following derivation:

VaR P = new Programmer () can obtain P. _ PROTO __= programmer. Prototype;

In the above example, we specify programmer. Prototype = new person (); let's split it like this, VAR p1 = new person (); programmer. Prototype = p1; then:

P1. _ PROTO __= person. Prototype;

Programmer. Prototype. _ PROTO __= person. Prototype;

Obtain P. _ PROTO __= programmer. prototype according to the preceding figure. You can obtain P. _ PROTO _. _ PROTO __= person. prototype.

Okay. Let's take a look at the above result, P. Say (). Since P does not have the say attribute. _ PROTO __, that is, programmer. prototype, which is found in P1. Since P1 does not have a say, it goes to P. _ PROTO __. _ PROTO __, that is, person. in prototype, the alert ("person say") method is found.

The rest are the same.

Next, let's look at the figure on stackoverflow:

I personally think that prototype can be regarded as a pointer. When a new method is used, it points to a memory area.

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.