zoukankan      html  css  js  c++  java
  • JavaScript面向对象之类的继承

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>JavaScript中的继承</title>
    
    <script type="text/javascript">
    // 父类
    function Person(name,email){   // 父类的构造方法
        this.name = name ;
        this.email = email ;
    }
    
    Person.prototype.getInfo = function(){
        return this.name+"的Email是:"+this.email ;
    }
    
    // 子类
    
    function Employee(name,email,title){
        // 如同高级程序语言中的super()方法,调用父类的构造方法,但必须传入scope为子类对象的实例
        Person.call(this,name,email) ;
        // 子类特色的属性
        this.title = title ;
    }
    // 如果要继承Person,子类的prototype必须指定父类的实例
    
    Employee.prototype = new Person () ;
    
    var emp = new Employee('1212','11222@qq.com','title1234');
    
    //  如果没有明确支持子类使用构造方法为父类的构造方法 
    console.info(Employee.prototype);
    
    console.info(emp.getInfo());
    
    delete Employee.prototype.name ;   // 如果子类不需要父类的属性可以通过delete进行删除
    //  如果子类和父类的构造方法处理事情不同可以明确的指出用子类的构造方法
    Employee.prototype.constructor = Employee ;
    
    console.info(Employee.prototype.constructor) ;
    </script>
    
    
    </head>
    
    <body>
    </body>
    </html>
  • 相关阅读:
    如何免费在 arm 官网上下载合适的手册
    ARM Cortex-A系列处理器性能分类比较
    USB OTG有关协议
    fseek在 fopen 带有'a'模式下不起作用
    Zynq 7000的3种IO
    多核处理器与MP架构
    Vim常用插件命令手册
    剑指 Offer 07
    Leetcode 94
    剑指offer 27
  • 原文地址:https://www.cnblogs.com/xiaotao726/p/4713110.html
Copyright © 2011-2022 走看看