博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js原生继承之——组合式继承实例
阅读量:7018 次
发布时间:2019-06-28

本文共 1178 字,大约阅读时间需要 3 分钟。

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>groupInherit</title>
    <script type="text/javascript">
    //声明父类
    function superClass(name){
        this.name = name;
        this.books = ['html','css','js'];
    }
    superClass.prototype.getName = function(){
        console.log(this.name);
    }
    superClass.prototype.getBooks = function(){
        console.log(this.books);
    }
    //声明子类
    function subClass(name,time){
        superClass.call(this,name);//让子this指向父this,后面带的是父类需传入的参数name
        this.time = time;
    }
    subClass.prototype = new superClass();//类式继承
    subClass.prototype.getTime = function(){
        console.log(this.time);
    }
    //测试用例:实例化对象测试
    var test1 = new subClass('js book',2015);
    var test2 = new subClass('css book',2014);
    test1.books.push('php');//test2插入的数据'php'不影响test1
    console.log(test1.name);      //'js book'
    console.log(test1.books);   //["html", "css", "js", "php"]
    test1.getName();              //'js book'
    test1.getBooks();           //["html", "css", "js", "php"]
    test1.getTime();            //2015
    console.log(test2.name);      //'css book'
    console.log(test2.books);   //["html", "css", "js"]
    test2.getName();              //'css book'
    test2.getBooks();           //["html", "css", "js"]
    test2.getTime();            //2014
    //本例已经通过验证,this属性和原型方法均能访问
    </script>
</head>
<body>
    
</body>
</html>

转载地址:http://ypzxl.baihongyu.com/

你可能感兴趣的文章
Linux中基于hadoop安装hive(CentOS7+hadoop2.8.0+hive2.1.1)
查看>>
线程的概念
查看>>
Win8 Metro(C#)数字图像处理--2.55OSTU法图像二值化
查看>>
对actuator的管理端点进行ip白名单限制(springBoot添加filter)
查看>>
异步模式
查看>>
MongoDB and GUI 管理界面
查看>>
sqlyog连接Linux上的mysql报错误号码2013,错误号码1130的解决办法
查看>>
获取Android APK JNI库
查看>>
linux系统卡解决方案
查看>>
窗口管理器 Openbox 入门指南
查看>>
Web-Fontmin -- 在线提取你需要的字体
查看>>
Java并发编程:Callable、Future和FutureTask
查看>>
Docker-Compose一键部署Ningx+Asp.net core站点+Redis
查看>>
四说大数据时代“神话”:从大数据到深数据
查看>>
25个经典的Spring面试问答
查看>>
使用阿里云Serverless函数计算实现HTTP健康检查+故障短信通知 ...
查看>>
Mysqlbinlog的一些操作和用法
查看>>
Oracle数据库数据类型
查看>>
MP实战系列(三)之实体类讲解
查看>>
12.2、python内置函数—format
查看>>