//继承的几种实现:
//解决方案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. 尝试asp.net mvc 基于controller action 方式权限控制方案可行性(转载)

    微软在推出mvc框架不久,短短几年里,版本更新之快,真是大快人心,微软在这种优秀的框架上做了大量的精力投入,是值得赞同的,毕竟程序员驾驭在这种框架上,能够强力的精化代码,代码层次也更加优雅,扩展较为方 ...

  2. Linux 下创建 sftp 用户并限定目录

    Linux 下创建 sftp 用户并限定目录 1.创建 sftpUser 用户组 [root@XXX ~]# groupadd sftpUser 2.创建 sftpUser 用户并指定目录 [root ...

  3. javap 反汇编class文件

    用法: javap 参数 class文件路径 其中, 可能的选项包括: -help --help -? 输出此用法消息 -version 版本信息 -v -verbose 输出附加信息 -l 输出行号 ...

  4. d3.js在vue项目中的安装及案例

    1. 安装: npm i d3 --save 2. 引入:main.js import * as d3 from "d3"; Vue.prototype.$d3 = d3; win ...

  5. 2. GitHub远程仓库

    1. GitHub ssh-keygen -t rsa -C "youremail@example.com"        之后再用户主目录里会有隐藏的.ssh目录,里面有id_r ...

  6. python安装mysqlclient模块报fatal error: Python.h:解决方法

    在搭建Flask框架安装mysqlclient模块时候老是报fatal error: Python.h:错误,折腾老半天,百度了老半天看了不少大神帖子,就是没解决, 后来发现这不是个BUG,都是自己的 ...

  7. 【xsy2818】 最近点 动态树分治+可持久化线段树

    题目大意:给你一颗n个节点的树,最初点集S为空. 有m次操作:往当前点集S中加入/删除一个点,询问点x至集合S中任意点的最小距离,回到第t次修改点集的操作后的状态. 数据范围:$n,m≤10^5$ 我 ...

  8. 调用的执行器“executor://mstestadapter/v2”时发生异常: 无法找到程序集“log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a”

    加上下面的一句就好了 [TestCleanup]      public void cleanup()      {          CallContext.FreeNamedDataSlot(&q ...

  9. WebForm——JS检测浏览器是否是IE浏览器

    function IEVersion() { var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 && userAgen ...

  10. swiper4-vue 不使用loop,由最后一张跳到第一张

    <template> <div class="swiper-box"> <div class="swiper-container" ...