• js 继承


    /**
     * 组合继承
     */
    function Super(name) {
        this.name = name || '';
    }
    Super.prototype.age = 23;
    
    function Sub(name) {
        Super.call(this, name);
        this.eat = function() {
            return this.name + 'eat';
        }
    }
    Sub.prototype = new Super();
    Sub.prototype.constructor = Sub
    var sub = new Sub('xiaoming');
    console.log(sub);

      

    /**
     * 寄生组合继承
     */
    function Super(name) {
        this.name = name || '';
    }
    Super.prototype.age = 23;
    
    function init(Sub, Sup) {
        var proto = Object(Sup.prototype);
        proto.constructor = Sub;
        Sub.prototype = proto;
    }
    function Sub(name) {
        Super.call(this, name);
        this.eat = function() {
            return this.name + 'eat';
        }
    }
    init(Sub, Super);
    // Sub.prototype = new Super();
    // Sub.prototype.constructor = Sub
    var sub = new Sub('xiaoming');
    console.log(sub);
    

      

    1、原型链继承

    2、构造函数继承

    3、组合继承

      其实就是结合了原型链继承和构造函数的继承

      缺点:之类继承父类的过程需要两次调用父类的构造器,这其实很浪费。

    4、组合寄生继承

      优点:解决了组合继承的缺点

      缺点:复杂,而且当你改变子类的prototype时,父类的prototype也会改变。

  • 相关阅读:
    python-多任务-进程
    注解_Annotation
    ZIP压缩输入/输出流
    什么是API,这篇文章让你豁然开朗
    异常处理(在控制台输入数据)
    控件监听与面板监听
    多态与继承
    Java——socketser与cli
    20170307
    20180305
  • 原文地址:https://www.cnblogs.com/pengwenfan/p/9012913.html
Copyright © 2020-2023  润新知