zoukankan      html  css  js  c++  java
  • 寄生组合式继承 js

    寄生组合式继承是集寄生式继承和组合继承的优点于一身,是基于类型继承最有效的方式
    function object(o){
        function F(){};
        F.prototype = o;
        return new F();
    }
    
    function inheritPrototype(subType,superType){
        var prototype = object(superType.prototype);
        prototype.constructor = subType;
        subType.prototype = prototype;
    }
    
    function SuperType(name){
        this.name = name;
        this.colors = ["red","yellow","green"];
    }
    
    SuperType.prototype.sayName = function(){
        alert(this.name)
    }
    
    function SubType(name,age){
        SuperType.call(this,name);
        this.age = age;
    }
    
    inheritPrototype(SubType,SuperType);
    
    SubType.prototype.sayAge = function(){
        alert(this.age);
    }
    
    var person1 = new SubType("china",1);
    console.log(person1.name);   // "china"
    console.log(person1.colors);  // ["red","yellow","green"]

    person1.sayAge(); // 1
  • 相关阅读:
    ES2017中的修饰器Decorator
    ES6中的模块
    代理(Proxy)和反射(Reflection)
    ES2017中的async函数
    ES6数字扩展
    Promise和异步编程
    ES6定型数组
    ES6数组扩展
    ES6中的类
    ES6中的Set和Map集合
  • 原文地址:https://www.cnblogs.com/cnundefined/p/7116689.html
Copyright © 2011-2022 走看看