ES5 Parasitic inheritance

3 Parasitic inheritance

  1. A combination of inherited problems called twice parent structure
  2. Prototypal inheritance problems can not instantiate the object can not pass the Senate
  3. A combination of inheritance and prototype inheritance there are problems subclass original prototype property is covered
  4. It is recommended to use Parasitic inheritance
 / *  
              Parasitic succession: 
                  Problems solved subclass 1 prototype object attributes are covered: 
                    setting F.property receiving parent class prototype attributes, the attributes to the class prototype replicon F.property 
                    F.peoperty then copy the prototype property subclass is provided to the sub-class prototype completed property cover 
                  2 solution call the parent class structural problems: 
                     the parent class as a property to prototype subclass, the subclass instance object to find a parent class constructor prototype chain 
                     subclasses by this calls itself an example of the method to achieve, not by calling the parent class constructor outside of mass participation 
                     so that is realized the function, but also to avoid a combination of inherited excessive number of calls to the parent class constructor and prototype inheritance can not be instantiated problem by constructing parameter passing 
        * / 
        function Box (the _r) {
             // set properties Box class object instantiated 
            the this .r = the _r; 
            the console.log ( "RUN constructor" ); 
        } 
        Box.a =. 3 ;
        Box.run = function () {
            console.log(Box.a);
        }
        Box.prototype = {
            b: 10,
            play: function () {

            }
        }

        function Ball(_r) {
            this.supClass.apply(this, arguments);
        }
        Ball.prototype.walk = function () {
            console.log("walk");
        }
        extend(Ball, Box);
        Ball.prototype.play = function () {
             the this .supClass.prototype.play.apply ( the this .arguments); 
            the console.log ( "Finish Method" ); 
        } 
        var B = new new Ball (10); // Print constructor RUN 
        b.play ();   // Print Method Finish 
        the console.log (B); 

        function Extend (subClass, supClass) {
             function F. () {}
             // subclass prototype attributes overwritten same parent class prototype attributes, and then set to the subclass 
            F.prototype = supClass.prototype;
             // copy the contents of the original prototype 
            if (Object.assign) {
                //Objec.asssign不兼容IE
                Object.assign(F.prototype, subClass.prototype);
            } else {
                if (Object.getOwnPropertyNames) {
                    var names = Object.getOwnPropertyNames(subClass.prototype);
                    for (var i = 0; i < names.length; i++) {
                        Object.defineProperty(F.prototype, name[i], Object.getOwnPropertyDescriptor(names[i]));
                    }
                }
                else {
                    for (varprop in subClass.prototype) { 
                        F.prototype [prop] = subClass.prototype [prop]; 
                    } 
                } 
            } 
            // complete attribute overrides 
            subClass.prototype = new new F. ();
             // subclass configured to set itself, does not call configured outside the parent class 
            subClass.prototype.constructor = subClass;
             // parent to child class prototype set up for this call 
            subClass.prototype.supClass = supClass;
             IF (supClass.prototype.constructor === Object) { 
                supClass. prototype.constructor = supClass;
            }
        }

 

Results of the:

 

Guess you like

Origin www.cnblogs.com/ltfxy/p/12350703.html