• JavaScript 面向对象的编程(三) 类的继承


    定义父类和子类的继承关系

    //声明父类
    function SuperClass(){
        this.superValue = true;
    }
    
    //为父类添加共有方法
    SuperClass.prototype.getSuperValue=function(){
        return this.superValue;
    }
    
    //声明子类
    function SubClass(){
        this.subValue = false;
    }
    
    //继承父类
    SubClass.prototype = new SuperClass();
    SubClass.prototype.getSubValue = function(){
        return this.subValue;
    }
    
    var instance = new SubClass();
    console.log(instance.getSuperValue());
    console.log(instance.getSubValue());

      console.log(instance instanceof SuperClass);
      console.log(instance instanceof SubClass);
      console.log(SubClass instanceof SuperClass);
      console.log(SubClass.prototype instanceof SuperClass);
      console.log(instance instanceof Object);

    将父类的实例赋给子类的prototype就实现了 类的继承

    类关系的检测

    结果如下:

    有版权问题请留言,或加我qq362601125

    参考列表

    1.《JavaScript设计模式》作者张荣铭

  • 相关阅读:
    前端 network
    C语言的安装及使用
    c语言
    mongodb
    大型网站--负载均衡架构
    双机热备ROSE HA工作原理
    Linux vmstat命令实战详解
    管理员必备的20个Linux系统监控工具
    linux top命令详解
    linux命令TOP参数load average详解[转]
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/8571540.html
Copyright © 2020-2023  润新知