The relationship between the prototype chain js, constructors, Prototype Example

Object-oriented programming inheritance concept will involve, JS implemented mainly through inheritance chain prototype method.

A relationship between the constructor Prototype Example

  Each create a function that automatically comes with a  prototype  property. This property is a pointer to an object , we call  the prototype object . What is a pointer? Pointer students like school, prototype object is the student. We find only the students through student number. Assuming that suddenly, the pointer is set null, student number is reset empty, do not panic, there are still objects, the students did not disappear. Just hard to find.

  The default prototype object has a property  constructor , this property is a pointer to its associated constructor.

  Generated by calling the constructor of example, has an internal property , it points to the prototype object. So instance can access all methods and properties of the prototype object.

  

  Therefore, the relationship between the three is that each has a prototype object constructor, the prototype object contains a pointer pointing to the constructor, and examples all contain a pointer to the prototype object. Popular point that is, instances can be accessed through the internal pointer to the prototype object, the prototype object through the constructor pointer, and can be found constructor.

  We see an example below:

1
2
3
4
5
6
7
8
9
function  Dog (name) {
     this .name = name;
     this .type =  'Dog'
}
Dog.prototype.speak =  function  () {
  alert( 'wang' );
}
var  doggie =  new  Dog( 'jiwawa' );
doggie.speak();   //wang 

   Above code defines a constructor Dog (), the prototype object Dog.prototype directed its own attributes but also refers back construtor Dog, i.e. Dog.prototype.constructor == Dog. Examples doggie due to its internal pointer to the prototype object, so to speak can access method.

 

  Dog.prototype just a pointer to a prototype object, but the object is not particularly prototype, it is also just an ordinary object. Let's say, at this time, we let Dog.protptype no longer point to the original prototype object, but another instance of the class (Animal), the situation will happen then?

 

Second, the prototype chain

  Earlier we said, all instances of an internal pointer to its prototype object, and all the attributes and methods can be accessed on a prototype object. Examples doggie Dog prototype object points to all the properties and methods Dog prototype object can access; if Dog prototype object into a certain class instance aaa, this example will point to a new prototype object of the AAA, then this doggie all properties and methods on instance attributes and AA a prototype object can access aaa. Similarly, the new prototype object is an instance bbb AAA happens to another object, this will point to the new instance bbb BBB prototype object, all the attributes and properties on the instance and then doggie BBB prototype object of this time you will be able to access bbb way to go.

  This is the inherited method JS achieved through the prototype chain. A look at the following example:

Copy the code
Animal // define a constructor Dog as a parent class 
function Animal () { 
    this.superType = 'Animal'; 
} 

Animal.prototype.superSpeak = function () { 
    Alert (this.superType); 
} 

function Dog (name) { 
    this.name = name; 
    this.type = 'Dog';   
} 
// pointer Dog prototype of change, a point Animal instance Dog.prototype = new new Animal ();
 // line above is equivalent to such a write 
// var = new new Animal Animal (); 
//Dog.prototype = Animal; 
Dog.prototype.speak = function () { 
  Alert (this.type); 
} 
var = new new Doggie Dog ( 'jiwawa'); 
doggie.superSpeak (); // Animal

Copy the code

  explain. The above code, defines a first Animal constructor () obtained by the new new instance of Animal, it will contain an instance of a prototype attributes and attribute superType superSpeak. In addition it defined a Dog constructor. Then the situation changes, the line code bold, the prototype object instance Dog animal became covered . When the doggie went on to visit superSpeak property, js will first look in the instance properties doggie, we found can not be found, then, js will go to doggie prototype object, the prototype object doggie we have been changed to an instance of animal , that is to find animal on the instance. Go first instance attribute animal was found or not superSpeack, and finally to the prototype object of animal go, eh, this find.

(I hope this chart to help you understand it.) 

  

  This shows that we can through the prototype chain to achieve all the properties and methods of Dog inherit Animal.

  In summary: When is rewritten prototype object Dog.prototype points, internal pointer instance also changed, pointing to the new prototype object, and then be able to achieve inheritance between class and the class. (However, if an instance of the object before rewriting the prototype, produced, or its internal pointer to the initial prototype object. The next time I send the article stresses. JS succession borrow constructors inherited )

Guess you like

Origin www.cnblogs.com/ltb6w/p/11546064.html