//继承的几种实现:
//解决方案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. spring mvc 的请求流程

    SpringMVC核心处理流程: 1.DispatcherServlet前端控制器接收发过来的请求,交给HandlerMapping处理器映射器 2.HandlerMapping处理器映射器,根据请求 ...

  2. WPF自定义Window窗体样式

    资源文件代码: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation ...

  3. Shell - 简明Shell入门08 - 函数(Function)

    示例脚本及注释 #!/bin/bash function Check() # 使用function定义函数 { Say # 通过函数名直接调用函数 if test $1 then return 0 # ...

  4. Data - Hadoop伪分布式配置 - 使用Hadoop2.8.0和Ubuntu16.04

    系统版本 anliven@Ubuntu1604:~$ uname -a Linux Ubuntu1604 4.8.0-36-generic #36~16.04.1-Ubuntu SMP Sun Feb ...

  5. WebRTC开发基础(WebRTC入门系列1:getUserMedia)

    什么是WebRTC WebRTC由IETF(Internet Engineering Task Force——互联网工程任务组)和W3C(World Wide Web Consortium——万维网联 ...

  6. java实现文件上传下载

    喜欢的朋友可以关注下,粉丝也缺. 今天发现已经有很久没有给大家分享一篇技术文章了,于是想了一下给大家分享一篇java实现文件上传下载功能的文章,不喜欢的希望大家勿喷. 想必大家都知道文件的上传前端页面 ...

  7. 课程一(Neural Networks and Deep Learning)总结——2、Deep Neural Networks

    Deep L-layer neural network 1 - General methodology As usual you will follow the Deep Learning metho ...

  8. Your Mac is infected with (3) Viruses!

    记一次流氓程序的清理 某天我的电脑不幸感染了这么一个病毒

  9. 常见数据结构的Java实现

    单链表的Java实现 首先参考wiki上的单链表说明,单链表每个节点包含数据和指向链表中下一个节点的指针或引用.然后看代码 import java.lang.*; public class Singl ...

  10. Django model转字典的几种方法

    平常的开发过程中不免遇到需要把model转成字典的需求,尤其是现在流行前后端分离架构,Json格式几乎成了前后端之间数据交换的标准,这种model转dict的需求就更多了,本文介绍几种日常使用的方法以 ...