js原生继承之——组合式继承实例
<!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>
js原生继承之——组合式继承实例的更多相关文章
- 详解js中的寄生组合式继承
寄生组合式继承是js中最理想的继承方式, 最大限度的节省了内存空间. js中的寄生组合式继承要求是: 1.子对象有父对象属性的副本, 且这些不应该保存在子对象的prototype上. 2. ...
- javascript中的继承-寄生组合式继承
前文说过,组合继承是javascript最常用的继承模式,不过,它也有自己的不足:组合继承无论在什么情况下,都会调用两次父类构造函数,一次是在创建子类原型的时候,另一次是在子类构造函数内部.子类最终会 ...
- js组合继承和寄生组合式继承比较
本文是原创文章,如需转载,请注明文章出处 1.js中实现组合继承(B继承A): function A(name){ this.name = name; this.ary = ["AA&quo ...
- 通过寄生组合式继承创建js的异常类
最近项目中在做js的统一的异常处理,需要自定义异常类.理想的设计方案为:自定义一个异常错误类BaseError,继承自Error,然后再自定义若干个系统异常,例如用户取消异常.表单异常.网络异常,这些 ...
- js原生继承几种方式
js原生继承 js本身并没有继承和类的概念,本质上是通过原型链(prototype)的形式实现的. 1.先写两个构造函数Parent和Child,用于将Child继承Parent function P ...
- js寄生组合式继承
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js组合式继承
组合式继承是比较经典的继承,但是也有比较严重的缺点就是连两次调用了父类的构造函数. <!DOCTYPE html> <html lang="en"> < ...
- [js高手之路]寄生组合式继承的优势
在之前javascript面向对象系列的文章里面,我们已经探讨了组合继承和寄生继承,回顾下组合继承: function Person( uName ){ this.skills = [ 'php', ...
- 寄生组合式继承 js
寄生组合式继承是集寄生式继承和组合继承的优点于一身,是基于类型继承最有效的方式 function object(o){ function F(){}; F.prototype = o; return ...
随机推荐
- 词链(link)
词链(link) 题目描述 给定一个仅包含小写字母的英文单词表,其中每个单词最多包含50个字母.如果一张由一个词或多个词组成的表中,每个单词(除了最后一个)都是排在它后面的单词的前缀,则称此表为一个词 ...
- CentOS 6.5添加163源
1.首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS ...
- css设置层级显示
效果: 代码: <li id="tabIdcontent4" class="nomal" tabid="content4" style ...
- android Service Activity三种交互方式(付源码)(转)
android Service Activity三种交互方式(付源码) Android应用服务器OSBeanthread android Service Binder交互通信实例 最下边有源代码: ...
- over-float清除浮动
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Struts2--默认Action
何时使用: 访问的网页不存在, 显示错误页面, 或者显示主页. 有时存在一种情况, 就是找不到对应action 可以在struts.xml里设置一个默认的action <?xml versio ...
- PAT (Advanced Level) 1027. Colors in Mars (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- springMVC入门配置及helloworld实例
1. 新建web project 2. 往lib里copy必须的jar 3. 改写web.xml <?xml version="1.0" encoding="UTF ...
- 单向链表(C#)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 关于Discuz与jQuery冲突问题的亲测解决方法
最近的一个项目整合dede和discuz程序,客户要求风格统一,所以有很多样式及特效都是要公用的.其中jQuery库定义的函数$()正好与discuz的comme.js中函数一样,这样就冲突了,导致d ...