javascript的继承模式
在javascript里面看到javascript的继承模式和传统的继承模式是有区别的,就想查资料看一下到底有区别,就看到了这篇文章,觉得讲得还可以,暂时先放上来,以后有别的东西再补充:
http://segmentfault.com/a/1190000000766541
基本模式
var Parent = function(){
this.name = 'parent';
};
Parent.prototype.getName = function(){
return this.name;
};
Parent.prototype.obj = {a:1};
var Child = function(){
this.name = 'child';
}
Child.protytype = new Parent();
var parent = new Parent();
var child = new Child();
console.log(parent.getName());//parent
console.log(child.getName());//child
这种事最简单实现原型继承的方法,直接把父类的对象复制给子类的构造函数的原型,这样子类的对象就可以访问到父类以及父类构造函数的protytype
这种方法的优点就是实现起来比较简单,不需要任何特殊的操作;同时他的缺点也很明显,如果子类需要做跟父类构造函数中相同的初始化动作,那么就得在子类构造函数中再重复一遍父类中的操作:
var Parent = function(name){
this.name = name || 'parent';
};
Parent.prototype.getName = function(){
return this.name;
};
Parent.prototype.obj = {a:1};
var Child = function(name)
{
this.name = name || 'child';
};
Child.prototype = new Parent();
var parent = new Parent('myParent');
var child = new Child('myChild');
console.log(parent.getName());//myParent
console.log(child.getName());//myChild
上述还只是初始化name属性,如果初始化工作不断增加,这种方式也不是很方便。
借用构造函数
var Parent = function(name){
this.name = name || 'parent';
};
Parent.prototype.getName = function(){
return this.name;
}
Parent.prototype.obj = {a:1};
var Child = function(name){
Parent.apply(this,arguments);
}
Child.prototype = new Parent();
var parent = new Parent('myParent');
var child = new Child('myChild');
console.log(parent.getNmae());//myParent
console.log(child.getName());//myChild
上面这种方法在子类构造函数中通过apply调用父类的构造函数进行相同的的初始化工作,这样不管负累做了多少初始化工作,子类可以执行同样的初始化工作。但是上面这种实现方法存在一个问题,父类构造函数被执行了两次,一次是在子类构造函数中,椅子是在赋值子类原型的时候,这是多余的,我们可以做以下改进:
var Parent = function(name){
this.name = name || 'parent';
};
Parent.prototypr.getName = function(){
return this.name;
};
Parent.prototype.obj = {a:1};
var Child = function(name){
Parent.apply(this,arguments);
};
Child.protytype = Parent.protytype;
var parent = new Parent('myParent')
var child = new Child('myChild');
console.log(parent.getName());//myParent
console.log(child.getName());//myChild
这样我们只需要在子类构造函数中执行一次父类的构造函数,同时又可以继承父类原型中的属性,这也比较原型的初衷,就是吧需要复用的内容放在原型中,我们可以继承原型中可复用的内容
临时构造函数模式
上面借用构造函数模式还是存在问题,它把父类的原型直接赋值给子类的原型,这会造成一个问题,就是如果对子类的原型做了修改,那么这个修改同时也会影响到父类的原型,进而影响父类对象。
var Parent = function(name){
this.name = name || 'parent';
};
Parent.prototype.getName = function(){
return this.name;
};
Parent.prototype.obj = {a:1};
var Child = function(name){
Parent.apply(this,arguments);
};
var F = new function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
var parent = new Parent('myParent');
var child = new Child('myChild');
console.log(parent.getName());//myParent
console.log(child.getName());/myChild
这样做事在子类原型和父类原型之间加入一个临时的构造函数F,切断了子类原型和父类原型之间的联系,这样子类原型的修改就不会影响到父类原型。
继续讨论
上面可以刻到的我们在Parent的prototype属性中加入一个obj对象字面量属性,但是一直没有使用
var Parent = function(name){
this.name = name || 'parent';
};
Parent.prototype.getName = function(){
return this.name;
};
Parent.prototype.obj = {a:1};
var Child = function(name){
Parent.aplly(this,arguments);
};
var F = new function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
var parent = new Parent('myParent');
var child = new Child('myChild');
console.log(child.obj.a);//1
console.log(parent.obj.a);//1
child.obj.a = 2;
console.log(child.obj.a);//2
console.log(parent.obj.a);//2
在上面这种情况,当我们把child对象obj.a修改的时候,同时父类的原型中的obj.a也会被修改。原因是因为当访问child.obj.a的时候,我们会沿着原型链一直找到父类的prototype中,然后找到了obj属性,然后对obj.a进行修改.
var Parent = function(name){
this.name = name || 'parent';
};
Parent.prototype.getName = function(){
return this.name;
};
Parent.prototype.obj = {a:1};
var Child = function(name){
Parent.aplly(this,arguments);
};
var F = new function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
var parent = new Parent('myParent');
var child = new Child('myChild');
console.log(child.obj.a);//1
console.log(parent.obj.a);//1
child.obj.a = 2;
console.log(child.obj.a);//2
console.log(parent.obj.a);//2
javascript的继承模式的更多相关文章
- 一文详解JavaScript的继承模式
1 原型链继承 #### ES6中通过原型继承多个引用类型的属性和方法,由于原型和实例的关系,即每个构造函数都有自己的原型对象,同时原型有一个属性指向构造函数,并且实例有一个内部的指针指向原型.如果存 ...
- 【读书笔记】读《JavaScript模式》 - 函数复用模式之现代继承模式
现代继承模式可表述为:其他任何不需要以类的方式考虑得模式. 现代继承方式#1 —— 原型继承之无类继承模式 function object(o) { function F() {}; F.protot ...
- 【读书笔记】读《JavaScript模式》 - 函数复用模式之类式继承模式
实现类式继承的目标是通过构造函数Child()获取来自于另外一个构造函数Parent()的属性,从而创建对象. 1.类式继承模式#1 —— 默认方式(原型指向父函数实例) function Paren ...
- 浅谈 JavaScript 中的继承模式
最近在读一本设计模式的书,书中的开头部分就讲了一下 JavaScript 中的继承,阅读之后写下了这篇博客作为笔记.毕竟好记性不如烂笔头. JavaScript 是一门面向对象的语言,但是 ES6 之 ...
- JavaScript的7种继承模式
<JavaScript模式>一书中,对于JavaScript的几种继承模式讲解得很清楚,给我提供了很大帮助.总结一下,有如下7种模式. 继承模式1--设置原型(默认模式) 实现方式: // ...
- javascript类式继承模式#4——共享原型
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- javascript类式继承模式#3——借用和设置原型
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- javascript类式继承模式#2——借用构造函数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- javascript类式继承模式#1——默认模式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
随机推荐
- UNIX网络编程总结二
绝大多数客户-服务程序使用TCP和UDP,这两个协议转而使用IP.UDP是一种简单的,不可靠的数据报协议,TCP是一种精致的可靠的字节流协议. 在TCP/IP协议族中: mrouted:IGMP→IP ...
- split 分割文件
1.命令功能 split将文件分割成多个碎片文件. 2.语法格式 split option input prefix split 选项 输入文件名 输出文件名前缀 参数说明 参数 参 ...
- BZOJ1135 LYZ(POI2009) Hall定理+线段树
做这个题之前首先要了解判定二分图有没有完备匹配的Hall定理: 那么根据Hell定理,如果任何一个X子集都能连大于等于|S|的Y子集就可以获得完备匹配,那么就是: 题目变成只要不满足上面这个条件就能得 ...
- Google Capture The Flag 2018 (Quals) - Reverse - Beginner's Quest - Gatekeeper
参考链接:https://ctftime.org/task/6264 题目 It's a media PC! All fully purchased through the online subscr ...
- 简单生成随机数id的方法
近期项目中需要生成不同的id,之前都是使用UUID来实现的,现在需求是只要8位数的,也就是说用户量是有限的,暂时是不需要太多的,所以就简单的使用Set实现了这一功能. /** * 生成8位不重复随机i ...
- 反射getDeclaredFields()
public static void main(String[] args) { // 获取所有属性值 Field[] fields = People.class.getDeclaredFields( ...
- MFC消息详解 (WindowProc|OnCommand|OnNotify)
1. 怎样使用MFC发送一个消息用MFC发送一个消息的方法是, 首先,应获取接收消息的CWnd类对象的指针: 然后,调用CWnd的成员函数SendMessage( ). LRESULT Res=pWn ...
- maven推送本地包到私服
前置要求:配置正确的settings.xml maven 推送 本地jar 到私服的命令示例: mvn deploy:deploy-file -DgroupId=com.oracle -Dartifa ...
- [POJ 1911] 棋盘
问题描述 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘.(每次切割都只能沿着 ...
- Callable使用示例
之前工作中也有使用过Callable,但是却没有使用Thread对象去操作过,今晚 小组培训,涨知识了,现特意记录一下,以免忘记. 先看一下Thread的构造器 可以看到,Thread类并没有提供参数 ...