JS in the inheritance, is object-oriented knowledge, because JS has no class concept. So inheritance is implemented through objects. Spoke of inheritance. We have to talk about prototype, we have to say the new process first.
A small piece of the following:
<script type="text/javascript"> var Person = function () { }; var p = new Person(); </script>
Let's see what this new has done. We can split the new process into the following three steps:
<1> var p={}; Other words. Initializes an object of P.
<2> P.Proto=person.prototype;
<3> Person.call (p); i.e. constructs p. can also be called initialization p.
The key is the second step, and let's prove it:
alert(p.__proto__ === Person.prototype);
This piece of code returns TRUE. Explain the correctness of step 2.
So what is proto ? Let's simply say here. Each object initializes a property within it. is Proto, when we visit the properties of an object. Assuming that the object does not exist within this property, then he will go to Proto to find this attribute, this proto will have their own Proto, so it has been looking down, which is what we usually call the concept of the prototype chain.
According to the standard,proto is not correct outside the public, that is, a private property , but the engine of Firefox exposes him to become a common property, we can access to ask and set.
<script type="text/javascript"> var Person = function () { }; Person.prototype.Say = function () { alert("Person say"); } var p = new Person(); p.Say(); </script>
This piece of code is very easy. I believe everyone has written this, so let's see why p can access the person's say.
First, var p=new person (), able to derive p. Proto=person.prototype. So when we call P. Say (), there is no Say this attribute in P, so he needs to find it in his Proto . That is person.prototype, and we define the Person.prototype.say=function () {} above; So, we found this method.
Let's take a look at some of the challenges below.
function tiger () { this . Bark=function ( { alert (); };}; //here first define a tiger method function cat () { this . Climb=function { alert ( "I will climb the tree" );}; //defines a cat method //how to inherit to let the Tiger also learn to climb trees? The following starts with inheritance.
tiger.prototype=new cat();var hnhu=new tiger();hnhu.climb();hnhu.valueof();//是不是非常奇妙,老虎也会怕会啦。嘿嘿
In combination with the above concept, I analyze the detailed inheritance process, first new a Tiger object. There are hnhu. Proto=tiger.prototype, having due to tiger.prototype=new cat ();
So tiger.prototype. Proto=cat.prototype. At this point the inheritance has paid the water. Convert to get:
Hnhu. Proto=tiger.prototype
Hnhu. Proto. Proto=cat.prototype
Well, after figuring it out, let's look at the results above, hnhu.climb (). Since Hnhu does not have climb this attribute. So go to Hnhu. Proto. That is tiger.prototype to find, because there is no climb in tiger.prototype, then go to Hnhu. Proto. Proto, which is Cat.prototype, finds the alert ("I'll climb the Tree");
The search for valueof () is also the same truth. This chain forms the prototype chain, and inheritance is realized through the prototype chain.
The above code illustrates:
This is the prototype and prototype chain. Similar to the scope and scope chain, you need to slowly savor the essence.
Mastered in the actual use of the. The Penguin succession interview question, probably means that a dog just started to whine. Then some kind of mutation occurs, and the call becomes mutated.
function dog(){ This. fark= function(){Alert"Whining"); };}; function Peter(){ This. money= function(){Alert"I'm a rich dog."); };}; Peter.prototype=NewDog ();p eter.prototype.bark= function(){Alert"Mutation");};vartz=NewPeter (); Tz.bark (); Tz.fark ();
You can also add some new properties to the Object.prototype. After joining, either the mutant dog has this attribute. Because object is in the second-to-last level of the prototype chain, the method above inherits its properties.
A specific explanation of the prototype inheritance in JavaScript