Js implemented inheritance (prototype / chain, camouflage function)

First, the example of the parent class prototype inheritance

        // parent class prototype and its property / method 
        function Supertype () {
             the this .name = [ ' ZC ' , ' LS ' , ' WW ' ]; 
        } 
        SuperType.prototype.getSuperName = function () {
             return  the this .name; 
        } ; 


        // child class and its prototype property / method for 
        function the SubType () {
             the this .test = [ ' A ' , ' B ' , ' C ' , ' D ']; 
        } 
        // subtype prototype points to an instance of the parent type (i.e., replication of the prototype subclass constructors and properties of the parent class prototype parent class / method) 
        SubType.prototype = new new Supertype (); 
         // subclass Add prototype prototype expansion property / method 
        SubType.prototype.getSubTest = function () {
             return  the this .test; 
        } 

        var instance1 = new new the SubType (); 
        instance1.name.push ( ' YZY ' ); // name attribute inherited from the prototype is examples of parent 
        instance1.test.push ( ' E ' ); // Test property itself is derived from a subclass constructor 
        console.log (instance1.name, instance1.test)

        var instance2 = new SubType();
        console.log(instance2.name,instance2.test)

Console output:

Mark:

① Note that points to an instance of a subclass prototype parent class (passed by reference), then this is an instance of the parent class of the memory address, after all, there will be a sub-class instance attributes prototype address this point, and the sub- A change also affects the type of data to address this subclass B.

Icon:

 

Guess you like

Origin www.cnblogs.com/eco-just/p/11106079.html