zoukankan      html  css  js  c++  java
  • 面向对象的程序设计(五)借用构造函数继承

    //借用构造函数(constructor stealing),有时候也叫伪造对象或者经典继承
    //弊端:原型方法不可复用,只有构造函数的属性可以复用
    function SuperType() {
        this.colors = ["red", "blue"];
    }
    
    function SubType() {
        SuperType.call(this);
        //SuperType.apply(this);
    }
    
    var instance1 = new SubType();
    instance1.colors.push("black");
    console.log(instance1.colors);//["red", "blue", "black"] 
    
    var instance2 = new SubType();
    console.log(instance2.colors);//["red", "blue"] 
    
    //相对于原型链,借用构造函数可以在子类型构造函数中向超类型构造函数传递参数
    function SuperType2(name) {
        this.name= name;
    }
    
    function SubType2(name, age) {
        //继承了SuperType2同时还传递了参数
        SuperType2.call(this, name);
        //SuperType2.apply(this, [name]);
    
        this.age = age;
    }
    
    var instance3 = new SubType2("Tom", "20");
    console.log(instance3.name);//Tom
    console.log(instance3.age);//20
  • 相关阅读:
    Pentaho
    sympy 解四元一次方程
    install R language on ubuntu
    pyside
    浙江省医院网上挂号
    mtu值相关
    Python 中除法运算需要注意的几点
    idea
    kilim
    good blog
  • 原文地址:https://www.cnblogs.com/qiangspecial/p/3175248.html
Copyright © 2011-2022 走看看