//JS All the functions have a prototype property functionAA () {} console.info (Aa.prototype); //This prototype property refers to an object, the prototype, that is initialized with an empty object, that is, without a member (that is, the prototype attribute and the prototype method). vari = 0; for(Iinchaa.prototype) {console.info (i); I++; } alert ("Member:" + i);//Alert shows the number of properties and methods of the prototype. 0 Properties and Methods functionAA () {} AA.prototype.name= ' Leyi '; AA.prototype.fn=function () { return' Hello world! ' }; varBB =NewAA (); //The instantiated object has no prototype object, and he can get the prototype attribute and prototype method on the prototype chain through __PROTO__.Console.info (Bb.prototype)//undefinedConsole.info (bb.__proto__)//point to Aa.prototype //then get the properties and methods of BB to get the method properties on the AA prototype through the prototype chainConsole.info (Bb.name = = = Bb.__proto__.name);//leyiConsole.info (Bb.fn () = = = Bb.__proto__.fn ());//Hello world! //The constructor function of BB is AA.prototype.constructorConsole.info (Bb.constructor = = = AA.prototype.constructor)//true //The prototype of BB's constructor function is Aa.prototypeConsole.info (Bb.constructor.prototype = = = Aa.prototype)//true //Inheritance //first, through the object impersonation to achieve inheritance functionX (width, height) { This. width =width; This. Height =height; } functionY (W, h) { This. fn = X;//methods that make the constructor of the parent class A subclass This. FN (W, h);//executes the method, inheriting the Width,height property of the X method Delete This. Fn;//Remove the method This. Draw =function() {Console.info ( This. Width + '---' + This. height);//Print inherited two properties } } NewY (+). Draw ();//--- //second, through call apply to achieve inheritance, the principle is similar to the above functionXX (width, height) { This. width =width; This. Height =height; } functionYY (W, h) {Xx.call ( This, W, h);//or Xx.apply (This,[w,h]); This. Draw =function() {Console.info ( This. Width + '---' + This. height);//Print inherited two properties } } NewYY (+). Draw ();//--- //third, through the prototype chain to achieve inheritance functionXXX (width, height) { This. fn =function() {Console.info (' Haha ') } } functionYYY () { This. Draw =function () { This. Fn (); }} Yyy.prototype=NewXXX (); varYYY =NewYYY () console.info (yyy.constructor)//Here's a little bit of a problem, yyy constructor function points to xxx //Yyy.prototype is the prototype of YYY, Yyy.constructor.prototype is the prototype of XXXConsole.info (Yyy.constructor.prototype = = = Yyy.prototype)//false //fixed pointYYY.prototype.constructor = YYY//point the constructor setting of the YYY prototype to the YYY constructorConsole.info (Yyy.constructor.prototype = = = Yyy.prototype)//trueConsole.info (Yyy.constructor)//YYYYyy.draw (); //Hybrid Inheritance functionXXXX (width, height) { This. width =width; This. Height =height; } XXXX.prototype.halo= ' halo! 'functionYYYY (W, h) {Xxxx.call ( This, W, h); This. Draw =function() {Console.info ( This. Width + '---' + This. Height); }} Yyyy.prototype=NewXXXX (); YYYY.prototype.constructor=YYYY; NewYYYY (666, 666). Draw ();//666---666Console.info (NewYYYY (). Halo)//halo! varCST =NewYYYY () console.info (Cst.constructor.prototype= = = Yyyy.prototype)//trueConsole.info (Cst.constructor)//YYYY
Reference:
Http://www.cnblogs.com/ljchow/archive/2010/06/08/1753526.html
http://javapolo.iteye.com/blog/1996871
JS Prototypes and Inheritance