• [javascript]寄生组合式继承


    子类需要继承超类的原型

    function object(o){
                    function f(){};
                    f.prototype = o;
                    return new f();
            }
    
                
    function inheritPrototype( subType ,superType ,proto){
                    var prototype = object( superType );
                    prototype.constructor = subType;
                    subType.prototype = prototype;
                    
                    for( p in proto ){
                        subType.prototype[p] = proto[p];
                }
            }

     创建互相继承的子类

    function Root(){
                this.name = "Root";
    }
    
    var RootPrototype = {
                reWrite : function(){return "root_rewrite";},
                sayName : function(){console.log(this.name);}
    };
    
    for(p in RootPrototype){
                Root.prototype[p] = RootPrototype[p];
    }
    
    
    function Note_1(){
                this.name = "Note_1";
    }
    
    var Note_1Prototype = {
                reWrite : function(){return "note_1_rewrite";},
                note_1_own : function(){console.log("note_1_own");}
    };
    
    function Note_1_1(){
                this.name = "Note_1_1";
    }
    
    var Note_1_1Prototype = {
                reWrite : function(){return "note_1_1_rewrite";},
                note_1_1_own : function(){console.log("note_1_1_own");}
    };

     继承

    inheritPrototype( Note_1 ,Root.prototype ,Note_1Prototype);
    inheritPrototype( Note_1_1 ,Note_1.prototype ,Note_1_1Prototype);

     测试

    var _root = new Root();
    var note_1 = new Note_1();
    var note_1_1 = new Note_1_1();
    
    console.log(_root);
    console.log(note_1);
    console.log(note_1_1);
    
    _root.sayName();
    note_1.sayName();
    note_1_1.sayName();
    
    note_1.note_1_own();
    note_1_1.note_1_1_own();
    
    
    console.log(note_1_1 instanceof Root);
    console.log(note_1_1 instanceof String);
    console.log(note_1 instanceof Root);
  • 相关阅读:
    bash 常用操作
    阿里云专有网络与弹性公网IP
    Excel 中 Index 和 Match 方法的使用
    分割excel sheet
    vba 工作案例-sheet间拷贝内容
    趣味题:重男轻女的村庄
    vba 工作案例1
    wordpress 导航相关的函数
    怎么样打印加密PDF文件
    excel 2013 图表制作
  • 原文地址:https://www.cnblogs.com/yiyide266/p/6934067.html
Copyright © 2020-2023  润新知