//继承的几种实现:
//解决方案1.通过原型继承
function Parent1(){
this.name = 'Parent1';
}
function Child1(){}
Child1.prototype = new Parent1();
//问题1:1.父类属性中存在引用类型属性时,会出现共享属性的情况(team属性被所有子类实行共享了)。
function Parent1(){
this.name = 'Parent1';//值类型
this.team = ['aa','bb','cc'];//引用类型
}
function Child1(){}
Child1.prototype = new Parent1();
var c1 =new Child1();
c1.name = 'lilei';
c1.team.push('dd');
console.info(c1.name);//lilei
console.info(c1.team);//["aa", "bb", "cc", "dd"]
var c2 = new Child1();
console.info(c2.name);//Parent1
console.info(c2.team);//["aa", "bb", "cc", "dd"]
//问题2.无法在不影响对象实例的情况下,对父类的构造函数传参
function Parent1(name){
this.name = name;
}
function Child1(){}
Child1.prototype = new Parent1('aa');
var c1 = new Child1();
console.info(c1.name);
Child1.prototype = new Parent1('bb');
var c2 = new Child1();
console.info(c2.name);
//问题3:此时子类的构造函数指向父类的构造函数
function Parent1(){
this.name = 'Parent1';
}
function Child1(){}
Child1.prototype = new Parent1();
console.info(new Child1().constructor);//ƒ Parent1(){ this.name = 'Parent1';}
//解决方案2.通过借用构造函数
function Parent2(){
this.name = 'Parent2';
}
function Child2(){
Parent2.call(this);
this.type = 'Child2';
}
//解决了父属性存在引用类型的问题
function Parent2(){
this.team = ['aa','bb','cc'];
}
function Child2(){
Parent2.call(this);
}
var c1 = new Child2();
c1.team.push('dd');
console.info(c1.team);//["aa", "bb", "cc", "dd"]
var c2 = new Child2();
console.info(c2.team);// ["aa", "bb", "cc"]
//问题:1、父类原型中的方法,子类不可见2.方法在构造函数中定义,函数无法复用。
//解决方案3.使用组合式继承
function Parent3(){
this.name = 'Parent3';
}
Parent3.prototype.talk = function(){
console.info('parent say hi');
}
function Child3(){
Parent3.call(this);//第二次调用Parent3()
this.type = 'Child3'
}
Child3.prototype = new Parent3();//第一次调用Parent3()
Child3.prototype.constructor = Child3();//解决构造函数问题
//问题:1.调用两次构造函数,(在新对象上创建的实例属性'name',屏蔽了原型中的同名属性)
//解决方案4:使用组合继承方法优化
function Parent4(){
this.name = 'Parent3';
}
function Child4(){
Parent4.call(this);//第二次调用Parent3()
this.type = 'Child3'
}
Child4.prototype = Parent4.prototype;
Child3.prototype.constructor = Child3();
//问题:Child4.prototype = Parent4.prototype;该句是将子类的原型直接指向了父类的原型,而从继承的本质上讲应该是父类的原型是子类原型的基础,因此使用到原型继承(克罗克富德提出)的写法Object.create().由此,产生了方案五:寄生组合继承。
//解决方案5:使用寄生组合继承方法
function Parent4(){
this.name = 'Parent3';
}
function Child4(){
Parent4.call(this);//第二次调用Parent3()
this.type = 'Child3'
}
Child4.prototype = Object.create(Parent4.prototype);
Child3.prototype.constructor = Child3();

JS继承实现的几种方式的更多相关文章

  1. 前端js,css文件合并三种方式,bat命令

    前端js,css文件合并三种方式,bat命令 前端js文件该如何合并三个方式如下:1. 一个大文件,所有js合并成一个大文件,所有页面都引用它.2. 各个页面大文件,各自页面合并生成自己所需js的大文 ...

  2. js获取时间戳的三种方式

      js获取时间戳的三种方式 CreateTime--2018年5月23日08:44:10 Author:Marydon // 方式一:推荐使用 var timestamp=new Date().ge ...

  3. js 函数定义的2种方式

      js 函数定义的2种方式 CreateTime--2018年3月29日18:36:14 Author:Marydon 方式一: /** * 函数式声明 */ function mode() { c ...

  4. js 复制文本的四种方式

    js 复制文本的四种方式 一.总结 一句话总结:js文本复制主流方法:document的execCommand方法 二.js 复制文本的四种方式 纯 转载复制,非原创 原地址:http://www.c ...

  5. js声明变量的三种方式

    JS 声明变量的三种方式 (1)使用变量步骤:a.声明-->b.赋值-->3.调用 正确用法: <script type="text/javascript"> ...

  6. JS对象创建的几种方式整理

    ​ 本文主要介绍了JS对象创建的几种方式 第一种:Object构造函数创建 var Person = new Object(); Person.name = 'Nike'; Person.age = ...

  7. 为Node.js编写组件的几种方式

    本文主要备忘为Node.js编写组件的三种实现:纯js实现.v8 API实现(同步&异步).借助swig框架实现. 关键字:Node.js.C++.v8.swig.异步.回调. 简介 首先介绍 ...

  8. Js 类定义的几种方式

    提起面向对象我们就能想到类,对象,封装,继承,多态.在<javaScript高级程序设计>(人民邮电出版社,曹力.张欣译.英文名字是:Professional JavaScript for ...

  9. js中创建对象的几种方式

    创建对象指创建一个object并给这个对象添加属性和方法,有以下几个方式: 最基本的: var Person={}; Person.name='tom'; Person.age='20'; Perso ...

随机推荐

  1. AlexNet详解2

    此处以caffe官方提供的AlexNet为例. 目录: 1.背景 2.框架介绍 3.步骤详细说明 5.参考文献 背景: AlexNet是在2012年被发表的一个金典之作,并在当年取得了ImageNet ...

  2. C# 不能用于文件名的字符

    在 Windows 有一些字符是不能作为文件名,尝试重命名一个文件,输入/ 就可以看到windows 提示的不能作为文件名的字符 那么具体是包括哪些符号不能作为文件名? Tilde (~) Numbe ...

  3. WPF ListBox的进阶使用(一)

    公司项目有个需求,UI界面支持动态平均分割界面,想了想便想到用ListBox来实现,用UniformGrid作为ListBox的ItemsPanelTemplate,通过动态改变UniformGrid ...

  4. sharepoint support ashx file

    Hello, I did the steps from the tutorial you are using. I have received the same error when I did no ...

  5. win10安装Ubuntu双系统

    1.软碟通做启动盘,不要用easyBCD,比较麻烦 2.windows10中取消选择"启用快速启动(推荐)" 3.压缩出空白卷 4.重启时按F12 5.在bios中将boot pr ...

  6. 【sping揭秘】4、某些无法注册到IOC容器的对象如何交给spring托管

    可以实现spring的factoryBean 接口,这样可以加入spring的IOC容器 比如现在有一个类叫MyObject,我们没有这个对象的源码,无法对这个对象进行操作,那么我们如何加入sprin ...

  7. docker学习实践之路[第二站]nginx镜像实践

    上一篇文章中已经成功的拉取的nginx的镜像 在本篇文章中则详细介绍docker利用文件卷.断后映射然后进行nginx的配置. 输入一下命令: docker run -d --name mynginx ...

  8. MVC3学习:利用mvc3+ajax检测用户是否被注册

    假设用户名是保存在表Users中.关系模式为Users(Uid,UserName,PassWord) 可先利用mvc自带的模板生成Create页面. 将填写用户名的地方,由原来的 <div cl ...

  9. Scala之隐式转换implicit详解

    假设我们有一个表示文本的行数的类LineNumber: class LineNumber ( val num : Int ) 我们可以用这个类来表示一本书中每一页的行数: val lineNumOfP ...

  10. 在Ubuntu Server上安装Postgresql

    首先更新一下源: sudo apt-get update 如果你不知道Postgresql具体的包的名称,可以使用一下语句进行查找: apt-cache search ^Postgresql 使用上述 ...