• Javascript的简单继承 简单


     function First(age, name) {
    this.age = age;
    this.name = name;
    }
    First.prototype.getAge = function() {
    return this.age;
    };
    First.prototype.toString = function() {
    return "[ age: " + this.age + ", name: " + this.name + " ]";
    };

    function Second(age, name, password) {
    First.call(this, age, name);
    this.password = password;
    }
    Second.prototype = new First();
    Second.prototype.constructor = Second;

    Second.prototype.getPassword = function() {
    return this.password;
    };
    Second.prototype.toString = function() {
    return "[ age: " + this.age + ", name: " + this.name + ", password: " + this.password + " ]";
    };
    //由于通过Second.prototype = new First();
    //继承了父类First的name和age属性,需要通过子类显示删除两个属性
    delete Second.prototype.age;
    delete Second.prototype.name;


    //继承First
    function Second2(age, name, sex) {
    First.call(this, age, name);
    this.sex = sex;
    }
    Second2.prototype.toString = function() {
    return "[ age: " + this.age + ", name: " + this.name + ", sex: " + this.sex + " ]";
    };
    Second2.prototype.getSex = function() {
    return this.sex;
    };

    function Third(age, name, password, address, email, sex) {
    Second.call(this, age, name, password);
    Second2.call(this, age, name, sex);
    this.address = address;
    this.email = email;
    }
    Third.prototype = new Second();
    Third.prototype.constructor = Third;
    Third.prototype.getAddress = function() {
    return this.address;
    };
    Third.prototype.setEmail = function(email) {
    this.email = email;
    };
    Third.prototype.toString = function() {
    return "[ age: " + this.age + ", name: " + this.name + ", password: " + this.password + ", address: " + this.address + ", email: " + this.email + " ]";
    };
    delete Third.prototype.name;
    delete Third.prototype.age;
    delete Third.prototype.password;

  • 相关阅读:
    json数据解析转文本方法
    百度HttpV3版本图片识别
    项目用Socket网络框架+Protobuf
    各类数据类型的转换类
    异形按钮点击触发
    通过名字找物体工具
    任意图形工具
    Debug日志可视化
    fread不能读完整个文件
    生产者消费者问题——C++ windows版 多生产者多消费者的队列实现
  • 原文地址:https://www.cnblogs.com/chyong168/p/2256051.html
Copyright © 2020-2023  润新知