ES5、ES6的四种继承方法

您所在的位置:网站首页 es5实现class继承 ES5、ES6的四种继承方法

ES5、ES6的四种继承方法

2024-07-09 02:06:15| 来源: 网络整理| 查看: 265

ES5、ES6的四种继承

众所周知,在ES6之前,前端是不存在类的语法糖,所以不能像其他语言一样,使用extends关键字就搞定继承关系,需要一些额外的方法来实现继承。

一. 原型链继承 function Parent(){ this.name = 'mortal' } Parent.prototype.getName = function(){ console.log(this.name) } function child(){ } //主要精髓所在 Child.prototype = new Parent() Child.prototype.constructor = Child let mortalChild = new Child() mortalChild.getName() //'mortal'

原型链继承缺点: 1.每个实例对引用类型属性的修改都会被其他的实例共享。

function Parent(){ this.names = ['mortal','mortal1'] } function Child(){ } //主要精髓所在 Child.prototype = new Parent() Child.prototype.constructor = Child let mortalChild2 = new Child() mortalChild2.name.push('mortal2') console.log(mortalChild2.names) //['mortal','mortal1','mortal2'] let mortalChild3 = new Child() mortalChild3.name.push('mortal3') console.log(mortalChild3.names) //['mortal','mortal1','mortal2','mortal3']

2.在创建Child实例的时候,无法向Parent传参。这样就会使Child实例没法自定义自己的属性(名字)

二. 借用构造函数(经典继承) function Parent(){ this.names = ['mortal','mortal1'] } function Child(){ Parent.call(this) } let mortalChild2 = new Child() mortalChild2.names.push('mortal2') console.log(mortalChild.names) //['mortal','mortal1','mortal2'] let mortalChild3 = new Child() mortalChild3.names.push('mortal3') console.log(mortalChild3.names) //['mortal','mortal1','mortal3']

优点: 1.解决了每个实例对引用类型属性的修改都会被其他的实例共享的问题。 2.子类可以向父类传值。

function Parent(name){ this.name = name } function Child(name){ Parent.call(this,name) } let mortalChild = new Child('mortal') console.log(mortalChild.name) //mortal let mortal2Child = new Child('mortal2') console.log(mortal2Child.name) //mortal2

缺点: 1.无法复用父类的公共函数 2.每次子类构造实例都得执行一次父类函数。

三. 原型式继承

复制传入的对象到创建对象的原型上,从而实现继承

function createObj(o){ function F(){} F.prototype = o return new F() } let person = { name:'mortal' body:['foot','hand'] } let person1 = createObj(person) let person2 = createObje('person') console.log(person1) //mortal person1.body.push('head') console.log(person2) //['foot','hand','head']

缺点:同原型链继承一样,每个实例对引用类型属性的修改都会被其他的实例共享。

ES6继承

ES6支持通过类来实现继承,方法比较简单,代码如下:

class Point{ constructor(x,y){ this.x = x this.y = y } toString(){ return this.x + '' + this.y } } class ColorPoint extends Point{ constructor(x,y,color){ super(x,y) //调用父类的constructor(x,y) this.color = color } toString(){ return this.color + ' ' + super.toString() //调用父类的toString() } } let colorPoint = new ColorPoint('1','2','red') console.log(colorPoint.toString()) //red 12


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭