js 实现继承的6种方式(逐渐优化)
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>实现继承的5种方式(逐渐优化)</title>
</head> <body> <script type="text/javascript">
//方法1 使用构造函数
function Parent1(name) {
this.name = name || 'meng';
} function Child1(age) {
Parent1.call(this)
this.age = age;
}
let c1 = new Child1(20)
console.log(c1)
//方法2 使用原型
function Parent2(name) {
this.name = name || 'meng';
// 对于这种是有问题的,因为子类共用一个原型
this.friend = [1, 2, 3]
} function Child2(age) {
this.age = age;
}
//不能使用Parent2.prototype,否则c2.friend.push会报错,因为Parent2.prototype的原型上没有friend属性
//Child2.prototype = Parent2.prototype
Child2.prototype = new Parent2()
let c2 = new Child2(21)
let c3 = new Child2(22)
c2.friend.push(4)
console.log(c2)
console.log(c3)
//组合继承
function Parent3(name) {
this.name = name || 'meng';
this.friend = [1, 2, 3]
} function Child3(age) {
Parent3.call(this)
this.age = age;
}
Child3.prototype = new Parent3()
let c4 = new Child3(21)
let c5 = new Child3(22)
c4.friend.push(4)
console.log(c4)
console.log(c5)
//组合继承优化1 只调用一次构造函数
function Parent4(name) {
this.name = name || 'meng';
this.friend = [1, 2, 3]
} function Child4(age) {
Parent4.call(this)
this.age = age;
}
Child4.prototype = Parent4.prototype
let c6 = new Child4(21)
let c7 = new Child4(22)
c6.friend.push(4)
console.log(c6)
console.log(c7)
//组合继承优化2 使用Object.create
function Parent5(name) {
this.name = name || 'meng';
this.friend = [1, 2, 3]
} function Child5(age) {
Parent4.call(this)
this.age = age;
}
Child5.prototype = Object.create(Parent5.prototype)
Child5.prototype.constructor = Child5
let c8 = new Child5(21)
let c9 = new Child5(22)
c8.friend.push(4)
console.log(c8)
console.log(c9)
//es6
class Parent6 {
constructor(name) {
this.name = name || 'meng'
this.friend = [1, 2, 3]
}
} class Child6 extends Parent6 {
constructor(name, age) {
super(name);
this.age = age
}
}
let c10 = new Child6(21)
let c11 = new Child6(22)
c10.friend.push(4)
console.log(c10)
console.log(c11)
</script>
</body> </html>
其中第五种方法:
Object.create这种方式实现了将父类和子类的的原型完美分隔 。双方不会互相影响,也就是说这是确实可行较好的继承实现方式。
js 实现继承的6种方式(逐渐优化)的更多相关文章
- js实现继承的5种方式 (笔记)
js实现继承的5种方式 以下 均为 ES5 的写法: js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承 ...
- js 实现继承的几种方式
//js中实现继承的几种方式 //实现继承首先要有一个父类,先创造一个动物的父类 function Animal(name){ this.name = name; this.shoot = funct ...
- js实现继承的两种方式
这是面试时面试官会经常问到问题: js的继承方式大致可分为两种:对象冒充和原型方式: 一.先说对象冒充,又可分为3种:临时属性方式.call().apply(): 1.临时属性方式: 当构造对象son ...
- js实现继承的5种方式
js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承有以下通用的几种方式1.使用对象冒充实现继承(该种实现 ...
- 深入浅出js实现继承的7种方式
给大家介绍7中js继承的方法 有些人认为JavaScript并不是真正的面向对象语言,在经典的面向对象语言中,您可能倾向于定义类对象,然后您可以简单地定义哪些类继承哪些类(参考C++ inherita ...
- JS实现继承的几种方式
前言 JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一.那么如何在JS中实现继承呢?让我们拭目以待. JS继承的实现方式 既然要实现继承,那么首先我们得有一个父类,代码如下: // 定义一个 ...
- JavaScript面向对象(三)——继承与闭包、JS实现继承的三种方式
前 言 JRedu 在之前的两篇博客中,我们详细探讨了JavaScript OOP中的各种知识点(JS OOP基础与JS 中This指向详解 . 成员属性.静态属性.原型属性与JS原型链).今天 ...
- JS 面向对象 ~ 继承的7种方式
前言: 继承 是 OO 语言中的一个最为人津津乐道的概念.许多 OO 语言都支持两种继承方式:接口继承 和 实现继承.接口继承只继承方法签名,而实现继承则继承实际的方法.如前所述,由于函数没有签名,在 ...
- JS实现继承的几种方式(转)
转自:幻天芒的博客 前言 JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一.那么如何在JS中实现继承呢?让我们拭目以待. JS继承的实现方式 既然要实现继承,那么首先我们得有一个父类,代码如 ...
随机推荐
- Vector HashMap List 存取数据速度
数组大小:40000List_List:0.0045List :0.0818List_HashMap:0.0072HashMap :0.0517List_Vector:0.0037Vector :0. ...
- 《Linux系统编程(第2版)》
<Linux系统编程(第2版)> 基本信息 作者: (美)Robert Love 译者: 祝洪凯 李妹芳 付途 出版社:人民邮电出版社 ISBN:9787115346353 上架时间:20 ...
- Orchard之在前台显式一个属于自己的列表
一:当前现状 Orchard 并不提供筛选 Owner 的 Query,但是 Gallery 中有提供,那就是:Owner Queries. Install 之,然后在解决方案中引入该 Project ...
- C/C++ 关于 for循环 的第二个表达式右侧非常量的时候
废话不多说,直接看代码: #include<stdio.h> int main(){ ; ;z<zmax;z++){ printf("i=%d z=%d\n",i ...
- Go语言之进阶篇请求报文格式分析
1. 请求报文格式分析 示例: package main import ( "fmt" "net" ) func main() { //监听 listener, ...
- Word Search leetcode java
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- css3新增样式介绍
在PC版开发中由于IE原因,我们很少用到css3,但随着平板和智能手机进入我们的生活,以及现在越来越流行,在手机版和平板版开发中我们就可以大胆的使用了,下面我们探讨常用几个css3属性: 1.css3 ...
- Java去掉Html标签的方法
content = content.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("< ...
- Android -- ConditionVariable
线程操作经常用到wait和notify,用起来稍显繁琐,而Android给我们封装好了一个ConditionVariable类,用于线程同步.提供了三个方法block().open().close() ...
- (转)AssetBundle系列——共享资源打包/依赖资源打包
有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...