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继承的实现方式 既然要实现继承,那么首先我们得有一个父类,代码如 ...
随机推荐
- geth 命令
{ admin: { datadir: "/home/.ethereum/.ethereum", nodeInfo: { enode: "enode://a974839a ...
- Dockerfile 指令汇总及解析
原文地址:http://www.maoyupeng.com/dockerfile-command-introduction.html 什么是Dockerfile Dockerfile是由一系列 ...
- Swift3.0:PhotoKit的使用
一.介绍 iOS8之前使用AssetsLibrary来获取相册资源,iOS新引入框架PhotoKit框架,也即Photos.framework 二.PhotoKit的基本构成包括如下几项: PHAss ...
- 奇怪吸引子---BurkeShaw
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- iOS开发-UILabel和UIButton添加下划线
关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选择 ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- jquery中对 iframe的操作
我们先看一下 JQUERY中的对像 contents() 的帮助文件 contents() 概述 查找匹配元素内部所有的子节点(包括文本节点).如果元素是一个iframe,则查找文档内容 示例 描述: ...
- nginx配置目录列表访问权限
我们知道apache httpd默认情况下会显示访问目录的文件列表,但是nginx访问时如果目录下面没有默认首页,那么会返回403 Forbidden的错误,表示没有权限访问,比如根目录就是nginx ...
- 以快板之名说Android 应用程序电源管理
当里个当,当里个当.Android开发UE(用户体验)为导向,首要任务便是省电量. 当里个当,当里个当.有一设备立足于墙边,这个设备唤固定电话.你的app造成这样,用户很快把你弃墙角.你咆哮耗电奈何与 ...
- Oracle中rownum用法警示
今天调试代码,发现分页查询时使用Oracle中rownum的between......and用法的bug,特此总结: 参考资料:http://blog.csdn.net/lg312200538/art ...