javascript代码复用--继承
由于javascript没有类的概念,因此无法通过接口继承,只能通过实现继承。实现继承是继承实际的方法,javascript中主要是依靠原型链要实现。
原型链继承
原型链继承是基本的继承模式,其本质是重写原型对象,使其为新对象的实例。代码实现如下:
function Person(){
this.name = "default";
var temp = "temp";
}
Person.prototype.age=0;
Person.prototype.getName = function(){
return this.name;
}
Person.prototype.getAge = function(){
return this.age;
}
console.log(Person.prototype.age);//
console.log(Person.age);//undefined
console.log(Person.prototype.name);//undefined
console.log(Person.name);//Person, if other property, should be undefined
function Student(){
this.type = "student";
}
//inheritance
Student.prototype = new Person();
console.log(Student.prototype.constructor);//Person(){}
console.log(Student.prototype.name);//default
Student.prototype.constructor = Student;
var student1 = new Student();
console.log(student1.getName());//default
console.log(student1.name);//default
console.log(student1.getAge());//
console.log(student1.age);//
console.log(student1.__proto__.age);//
console.log(student1.temp);//undefined
console.log(student1 instanceof Object);//true
console.log(student1 instanceof Person);//true
console.log(student1 instanceof Student);//true
console.log(Student instanceof Person);//false
以上代码主要注意两个问题:
1.函数局部变量,内部属性及原型属性的区别。var temp定义了一个局部变量,this.name定义了一个内部属性,prototype.age则定义了一个原型属性。
对于局部变量,无法在函数以外的地方调用,包括实例。
之前说过,函数本身的prototype属性仅仅用于函数实例的属性继承,而函数本身不会使用这个关联的prototype,在prototype中设置的属性将直接作用于所有实例。(比如Person的实例Student.prototype和student1,注意Student并不是Person的实例)
而对于函数内部属性,函数实例将直接拥有对应的内部属性(初始值),而无法通过函数本身使用内部属性。这一点其实跟prototype属性有所区别。
2.利用重写原型对象实现继承的时候,Student.prototype = new Person(), Student.prototype将指向了另一个对象Person.prototype,因此此时Student.prototype.constructor将指向Person函数。通过Student.prototype.constructor = Student 可以将其constructor重新指向Student。
通过原型链可以更好的理解上面的代码:

原型链继承的缺点
关于原型链继承的问题,其实就是跟通过原型方式创建对象的问题一样,就是原型中包含引用类型所带来的共享问题。
还有就是创建实例的时候,无法向构造器中传递参数。
构造函数继承
另一种经典的继承便是通过构造函数实现继承,即通过apply()和call()方法在子类构造函数内部调用父类构造函数。具体实现如下:
function Person(name){
this.name = name;
this.friends = new Array();
}
Person.prototype.age = 0;
function Student(name){
Person.call(this, name);
}
var student1 = new Student("Huge");
student1.friends.push("Alan");
console.log(student1.name);//Huge
console.log(student1.age);//undefined
console.log(student1.friends);//["Alan"]
var student2 = new Student("Heri");
student2.friends.push("Amly");
console.log(student2.name);//Heri
console.log(student2.friends);//["Amly"]
console.log(student1 instanceof Person);//false
console.log(student1 instanceof Student);//true
通过构造函数继承的问题除了构造函数模式本身存在的缺点之外(重复实例化方法),也无法类型识别,因此在父类原型中定义的方法和属性无法在子类中调用。
组合继承
由于通过原型链继承和构造函数继承都有其优缺点,因此将这两种继承方式组合起来,使用原型链继承实现原型中方法和属性的继承,通过构造函数继承实现参数传递和引用类型继承,是javascript中最常用的继承模式。代码实现如下:
function Person(name, age){
this.name = name;
this.age = age;
this.friends = new Array();
}
Person.prototype.getName = function(){
return this.name;
}
function Student(name, age){
this.type = "student";
Person.call(this, name, age);
}
Student.prototype = new Person();
Student.prototype.constructor = Student;
var student1 = new Student("Huge", 15);
student1.friends.push("Alan");
console.log(student1.name);//Huge
console.log(student1.age);//
console.log(student1.friends);//["Alan"]
console.log(student1.getName());//Huge
console.log(student1 instanceof Person);//true
console.log(student1 instanceof Student);//true
var student2 = new Student("Heri", 16);
student2.friends.push("Amly");
console.log(student2.name);//Heri
console.log(student2.age);//
console.log(student2.friends);//["Amly"]
console.log(Student.prototype.name);//undefined
console.log(Student.prototype.friends);//[]
从代码可以看出,组合继承会调用两次父类的构造函数:创建子类原型的时候和在子类构造函数内部调用。实际上,第一次创建子类原型的时候,子类已经包含了父类对象的全部实例属性,因此当通过调用子类构造函数创建实例的时候,将会重写这些属性。即同时存在两组属性,一组在实例上,一组在子类原型中,如上代码中Student.prototype.friends返回的空数组。这就是调用两次父类构造函数的结果。
其他继承方式
Crockford曾经提出了prototypal inheritance以及与之结合的parasitic inheritance。通过原型创建基于原有对象的新对象,并为新对象增加功能。
//prototypal inhertance
function createObject(obj){
function F(){}
F.prototype = obj;
return new F();
}
//parasitic inheritance
function enhanceObject(obj){
var enhanceObj = createObject(obj);
enhanceObj.getName = function(){
return this.name;
}
return enhanceObj;
}
var person = {
name : "Alan"
};
var person1 = enhanceObject(person);
console.log(person1.getName());//Alan
更进一步,为了避免组合继承模式两次调用父类构造函数的问题,可以利用parasitic inheritance来继承父类的原型,再将其指定给子类的原型。如下代码:
//prototypal inhertance
function createObject(obj){
function F(){}
F.prototype = obj;
return new F();
}
//parasitic inheritance
function inheritPrototype(superObj, subObj){
var obj = createObject(superObj.prototype);
obj.constructor = subObj;
subObj.prototype = obj;
}
function Person(name, age){
this.name = name;
this.age = age;
this.friends = new Array();
}
Person.prototype.getName = function(){
return this.name;
}
function Student(name, age){
this.type = "student";
Person.call(this, name, age);
}
inheritPrototype(Person, Student);
var student1 = new Student("Huge", 15);
student1.friends.push("Alan");
console.log(student1.name);//Huge
console.log(student1.age);//
console.log(student1.friends);//["Alan"]
console.log(student1.getName());//Huge
console.log(student1 instanceof Person);//true
console.log(student1 instanceof Student);//true
var student2 = new Student("Heri", 16);
student2.friends.push("Amly");
console.log(student2.name);//Heri
console.log(student2.age);//
console.log(student2.friends);//["Amly"]\
console.log(Student.prototype.name);//undefined
console.log(Student.prototype.friends);//undefined
可以看出,子类只调用了父类一次构造函数,避免在子类原型中创建不必要的属性。同时,原型链也保持不便,可以说是实现类型继承的最有效方式。
javascript代码复用--继承的更多相关文章
- C++进阶--代码复用 继承vs组合
//############################################################################ /* * 代码复用: 继承 vs 组合 * ...
- javascript代码复用(四)-混入、借用方法和绑定
这篇继续说js的现代复用模式:混入.借用方法和绑定. 混入 可以针对前面提到的通过属性复制实现代码复用的想法进行一个扩展,就是混入(mix-in).混入并不是复制一个完整的对象,而是从多个对象中复制出 ...
- javascript代码复用模式(二)
前面说到,javascript的代码复用模式,可分为类式继承和非类式继承(现代继承).这篇就继续类式继承. 类式继承模式-借用构造函数 使用借用构造函数的方法,可以从子构造函数得到父构造函数传任意数量 ...
- javascript代码复用模式
代码复用有一个著名的原则,是GoF提出的:优先使用对象组合,而不是类继承.在javascript中,并没有类的概念,所以代码的复用,也并不局限于类式继承.javascript中创建对象的方法很多,有构 ...
- javascript代码复用模式(三)
前面谈到了javascript的类式继承.这篇继续部分类式继承,及一些现代继承. 类式继承模式-代理构造函数 这种模式通过断开父对象与子对象之间原型之间的直接链接关系,来解决上次说到的共享一个原型所带 ...
- javascript 模式(1)——代码复用
程序的开发离不开代码的复用,通过代码复用可以减少开发和维护成本,在谈及代码复用的时候,会首先想到继承性,但继承并不是解决代码复用的唯一方式,还有其他的复用模式比如对象组合.本节将会讲解多种继承模式以实 ...
- 《JavaScript模式》第6章 代码复用模式
@by Ruth92(转载请注明出处) 第6章:代码复用模式 GoF 在其著作中提出的有关创建对象的建议原则: -- 优先使用对象组合,而不是类继承. 传统模式:使用类继承: 现代模式:"类 ...
- 初涉JavaScript模式 (13) : 代码复用 【上】
引子 博客断了一段时间,不是不写,一是没时间,二是觉得自己沉淀不够,经过一段时间的学习和实战,今天来总结下一个老生常谈的东西: 代码复用. 为何复用 JS门槛低,故很多人以为写几个特效就会JS,其实真 ...
- 【前端学习】javascript面向对象编程(继承和复用)
前言 继承,代码复用的一种模式.和其它高级程序语言相比,javascript有点点不一样,它是一门纯面向对象的语言,在JS中,没有类的概念,但也可以通过原型(prototype)来模拟对象 ...
随机推荐
- 代码片段---判断一文件是不是字符设备如果是把它拷贝到 /dev目录下
#!/bin/sh myfile=/home/liu 这个是文件的路径 fd = `ls -l myfile` 获取文件的所有属性 fp= ${fd::} if ["$fp" = ...
- Transact-SQL三值逻辑
/*===========================<一>========================== 在SQL中逻辑表达式的值有三种: 1.TRUE 2.FALSE 3.U ...
- rsync同步工具学习笔记
rsync同步工具 1.rsync介绍 rsync是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据同步备份的优秀工具.rsync软件适用于unix/linux/windows等多种操作 ...
- C# mvc--ORM框架中EF的作用和特点
存放于System.Linq.QueryAble 静态类中 并且所有的扩展方法扩展自 IqueryAble<TSource>泛型接口上 用途: 接收lambda表达式 利用EF生成对应的s ...
- Java基础知识强化之IO流笔记83:NIO与IO
当学习了Java NIO和IO的API后,一个问题马上涌入脑海: 我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异.它们的使用场景,以及它们如何影响您的代 ...
- 控制Input框输入的为大写字母
本来我的想法是Jquery来控制的,万万没想到...用Css就可以实现!!!! .toUp input{ text-transform:uppercase; } 感谢: http:// ...
- iOS UINavigationController 详解
developer.apple.com/cn/ 导航条 UINavigationBar继承UIView 导航控制器 UINavigationController (压栈,出栈) ...
- 深入理解HTTPS通讯原理
一.HTTPS简介 HTTPS(Hyper Text Transfer Protocol over Secure Socket Layer),简单来讲就是加了安全的HTTP,即HTTP+SSL:我们知 ...
- mysql-DDL-创建数据库
创建一个数据库 • 建立数据库操作: 语法: create database 数据库名 叙述:创建一个具有指定名称的数据库.如果要创建的 数据库已经存在,或者没有创建它的适当权限,则此 语句失败. 例 ...
- UIBootatrap:是由AngularJS UI团队编写的纯AngularJS实现的Bootstrap组件
本文为翻译文档.原文是https://angular-ui.github.io/bootstrap/(需要FQ). 准备工作: 依赖关系:这个库中包含一组基于Bootstrap组件和CSS的原生Ang ...