I understand the prototype in JavaScript and __proto__, constructor

Source: Internet
Author: User

A few days ago when I was also looking forward to talking about node. js, I gradually found that JS is a rich language-I have been in the bottom of the hole (beginner's sorrow). When I wanted to learn JavaScript seriously, I found that prototype and __proto__ and constructor were always frustrating to me (learning a language attribute and learning a P). But I am a not easy to concede, nor will not compromise, that is, there is this habit, I will go to do to understand even more frustration. So I searched the internet for explanations and interpretations of the many things that bothered me. The end result is--or do not understand. "What object prototype, what internal prototype", is it the Monkey King? So changeable. Later in the "JavaScript language Essence and Programming practice lite" When suddenly let me know the true face of __proto__ and prototype.

Here I draw a picture ([url]http://anykoro.sinaapp.com/2012/01/31/javascript%e4%b8%adfunctionobjectprototypes__proto__ and other concepts] [ /URL]) Other articles if you want to really understand not to see, the more you see the more confused (some people do not understand or half understand to the above three guys to define the mess, make me foggy, fraught). I am here only with the analogy, said I said prototype and __proto__ and constructor is I understand the model, not to give him the definition of death, each person's understanding is can be different knowledge not to engage in so specialization, like you can give him the definition like (install the great God, fraught).

The prototype that function can invoke is generally defined as "the reference to the object prototype", and some people say it is an "instance" (I was really drunk with the idea of Java at first). My understanding of prototype is "Papa", which is the mold of the object. When we add a property to the prototype, the "mold" changes its shape, and the example created later with the mold (var x = new X;) will be done by the mold, so that his instance will have prototype properties. If we use the form of "x.prototype=" to change its mold, that is, to change a "dad", so this will not be a little change, but instead of (to find a stepfather). So when you execute this sentence, you change the attributes of the child that is born in X. If you still do not understand the link I gave you to add their own experiments.

__proto__ In my understanding is the "shadow" or the attribute view of the parent class. with "var x = function () {} or function X () {} var x = new X ();" The __proto__ attribute in X will point to X.prototype. This is the most identical of the prototype chain, whether prototype change or __proto__ change, the chain is always equal.

<code>

Some of the original examples are false. There is a slight improvement in his example.

function Foo ()
{

}

foo.__proto__.test= "__proto__ test property found";//extended by __proto__
Foo.__proto__.addextend=function () {alert ("Foo add extend by __proto__");}
alert (Function.prototype.test);//can be accessed, why is the object here Function.prototype? I believe foo.__proto__ is a son, and who is dad? Function.prototype

var foo=new foo;
Foo.__proto__.lab = "ABC";
alert (Foo.__proto__===foo.prototype);

</code>

The above code on the __proto__ foo to add (in fact, the change of foo.__proto__ according to the truth that the corresponding Function.prototype.test,foo __proto__ is corresponding to the prototype of Foo, This relationship is always identical), and __proto__ is a special shadow or view (like an Oracle database), you modify it to affect the prototype of function, if you add it after the above code

<code>

for (var x in foo.__proto__) {
alert (x);
}
for (Var y in Foo.prototype) {
alert (Foo.prototype.lab);
}
alert (Foo.lab);

</code>

is accessible to the lab (Chinese Translation: Experiment) this property. So, in this case, __proto__ is similar to a view in a database, which is peculiar as a son in turn affects his father (a good family).

While understanding constructor can be prototype as an example, like Foo's prototype (instance) of the constructor is not pointing to the function itself! That is to say, Foo.prototype.constructor is the same as Foo.constructor. And Foo.constructor is a function (this can be interpreted as constructing Foo's constructor is not the new function ()) Here in fact, for the understanding of prototype I also can not be very good to unify, we can only understand their own (separate understanding will be more clear).

In short, I think (in my JavaScript position for a few days) that Foo.prototype is the __proto__ "Dad" or "true" of the new instance, and foo.__proto__ is the "shadow" or "View" of the class. (special), when the father changed the son of course different, but the son change is the father will also change (I can only understand, I really don't know). And constructor is the property of the construction object, note that the object (everything in JavaScript object, so function itself is also an object, there will be constructor, all the function is the big function (through the new function (); The construction of the small Foo is of course Big Foo, and the constructor of the case of Big Foo (prototype) is, of course, Big Foo. That's why Foo's prototype constructor point to his own story. Complete! PS: Although I have just learned JavaScript, I think this understanding of these three things, is the only way I can accept the understanding, there is a different understanding and do not produce ambiguity in their own way of understanding. If you still feel confused after watching, just follow your own.

I understand the prototype in JavaScript and __proto__, constructor

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.