Let's look at a piece of code like this:
Copy Code code as follows:
<script type= "Text/javascript" >
var person = function () {};
var p = new person ();
</script>
A very simple piece of code, let's see what this new one does. We can split the new process into three steps:
<1> var p={}; In other words, initialize an object p.
<2> P.__proto__=person.prototype;
<3> Person.call (P), in other words, construct p, which can also be called initialization p.
The key is the second step, and let's prove it:
Copy Code code as follows:
<script type= "Text/javascript" >
var person = function () {};
var p = new person ();
Alert (p.__proto__ = = Person.prototype);
</script>
This piece of code returns TRUE. Explain the correctness of our step 2.
So what's __proto__? We are here to say simply. Each object initializes an attribute within it, which is __proto__, and when we access an object's properties, if it does not exist inside the object, then he goes to the __proto__ to find the attribute, and the __proto__ has its own __proto__, So keep looking, that is, we usually say the concept of the prototype chain.
According to the standard, __proto__ is not public, that is, a private property, but the Firefox engine exposed him to become a common attribute, we can access and set up.
Well, the concept is clear, let's take a look at the following code:
Copy Code code as follows:
<script type= "Text/javascript" >
var person = function () {};
Person.prototype.Say = function () {
Alert ("person say");
}
var p = new person ();
P.say ();
</script>
This code is very simple, I believe everyone wrote this, so let's see why p can access the person's say.
First, var p=new person () can draw p.__proto__=person.prototype. So when we call P. Say (), first p does not Say this property, so he needs to go to his __proto__ to find, that is, person.prototype, and we have defined the above person.prototype.say=function () {}; So, we found this method.
Okay, next, let's look at a more complicated one.
Copy Code code as follows:
<script type= "Text/javascript" >
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);
</script>
Let's do this derivation:
var p=new programmer () can draw p.__proto__=programmer.prototype;
And in the above we specify the programmer.prototype=new person (); Let's split this, var p1=new person (); PROGRAMMER.PROTOTYPE=P1; then:
P1.__proto__=person.prototype;
Programmer.prototype.__proto__=person.prototype;
From the above to get p.__proto__=programmer.prototype. Can get p.__proto__.__proto__=person.prototype.
Okay, well, we'll look at the results above, P.say (). Because P did not say this attribute, so go to p.__proto__, that is, Programmer.prototype, that is, p1 to find, because there is no P1 say, then go to p.__proto__.__proto__, That is to find the Person.prototype, and then found the alert ("Person say") method.
The rest is the same.
This is the realization principle of the prototype chain.
Finally, in fact, prototype is only an illusion, he is in the realization of the prototype chain is only a subsidiary role, in other words, he is only in New times have a certain value, and the essence of the prototype chain is actually __proto__!