JavaScript's prototype chain;
JavaScript prototype chain, I would like to understand it, but has not understood, today seriously studied the next tutorial, the prototype chain of their own understanding of the language under the description;
There is a basic pattern for implementing a prototype chain, and its code is basically as follows:
functionsupertype () { This. property =true;} SuperType.prototype.getSuperValue=function(){ return This. Superproperty;};functionsubtype () { This. Subproperty =false;}//inherited super.Subtype.prototype =Newsupertype (); SubType.prototype.getSuperValue=function(){ return This. Subproperty;} varInstance =Newsubtype (); alert (Instance.getsupervalue)//true;
The search process is probably as follows: find it on the instance first, and then look it up in the prototype object. is always a ring buckle. Until the end of the prototype chain can be stopped;
1.1 Don't forget the default prototype;
The default prototype for all functions is an instance of object, so the default prototype will have a pointer to Object.prototype. This is why all custom types inherit the ToString () method;;
1.2 Determine the relationship between the prototype and the instance;
The relationship between the prototype and the instance can be judged in two ways; One is the instanceof operator, as long as this operator, to test the instance and the occurrence of the constructor;
There is also a way to isprototypeof ();
Alert (object.prototype.isPrototypeof (instance)); // truealert (Supertype.prototype.isPrototypeof (instance)); // true; Alert (Subtype.prototype.isPrototypeof (instance));//true;
1.3 a prudent definition method;
To override the method of a superclass or to add a method of a superclass, be sure to replace the superclass's instance with a subclass of prototype to add or modify the method, and a point where the modified method is called only when the subclass is called, and the superclass's
Instance calls or the original method; The reason I understand is that the superclass is just a prototype of the subclass, and the prototype of the superclass is still unchanged;
There is also a note that the prototype can not be defined by the object literal when it is implemented by the prototype chain, because it cuts off the connection between subclasses and superclass;
The prototype of the sub-class is directed directly to an instance of object;
1.4 Problem of the prototype chain;
The inheritance of JavaScript