zoukankan      html  css  js  c++  java
  • Javascript继承3:将优点为我所有----组合式继承

    //声明父类
    function ParentClass(name){
        //值类型公有属性
        this.name = name
        //引用类型公有属性
        this.books = ['Html']
    }
    //父类型原型公有方法
    ParentClass.prototype.getName = function(){
        console.log(this.name);
    }
    //声明子类
    function ChildClass(name,id){
        //构造函数式继承父类name属性
        ParentClass.call(this,name);
        //子类中新增公有属性
        this.id = id;
    }
    // 类式继承  子类原型继承父类
    ChildClass.prototype = new ParentClass();
    // 子类原型方法
    ChildClass.prototype.getId = function(){
        console.log(this.id);
    }
    
    var child1 = new ChildClass('Css',1)
    child1.books.push('图解Css');
    console.log(child1.books) // ['Html','图解Css']
    child1.getName()          // Css
    child1.getId()            // 1
    
    
    var child2 = new ChildClass('Javascript',2)
    console.log(child2.books) // ['Html']
    child2.getName()          // Javascript
    chil2.getId()             // 2

    设计模式中的经典笔录

  • 相关阅读:
    实战演练:通过伪列、虚拟列实现SQL优化
    python try else
    Prince2是怎么考试的?
    Prince2是怎么考试的?
    Prince2是怎么考试的?
    Prince2是怎么考试的?
    拦截器
    拦截器
    拦截器
    拦截器
  • 原文地址:https://www.cnblogs.com/-walker/p/9737390.html
Copyright © 2011-2022 走看看