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继承的实现方式 既然要实现继承,那么首先我们得有一个父类,代码如 ...
随机推荐
- 哥谭第四季/全集Gotham迅雷下载
<哥谭>(Gotham)第三季刚刚结束,第四季首集的集名就公布了.<Pax Penguina>这个集名在拉丁语中意味着「Pax Romana」,也就是「罗马式的和平」(Roma ...
- 深入理解Java并发之synchronized实现原理
深入理解Java类型信息(Class对象)与反射机制 深入理解Java枚举类型(enum) 深入理解Java注解类型(@Annotation) 深入理解Java类加载器(ClassLoader) 深入 ...
- [Android] Implementation vs API dependency
原文链接: https://jeroenmols.com/blog/2017/06/14/androidstudio3/ https://blog.csdn.net/lonewolf521125/ar ...
- iOS:创建带logol的二维码
//二维码生成 实质: 把字符串转变为 图片 // 需要 coreImage框架, 已经包含在了 UIKit框架里面 //MARK: 二维码中间内置图片,可以是公司logo + (UIImage *) ...
- libnids使用举例
---[[ libnids应用实例 ]]---------------------------------- 1.nids_next()函数的应用 ========================== ...
- 安装Lync 2013过程中遇到的第一个报错
安装Lync 2013, 首先要去做的就是prepare AD Forest. 在使用向导的时候会遇到报错如下: Prepare Forest Active Directory setting exe ...
- 自定义View 水印布局 WaterMark 前景色 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- CSS渐变字体、镂空字体、input框提示信息颜色、给图片加上内阴影、3/4圆
1.渐变字体 主要是看:-webkit-background-clip: text; 该属性 <style> .b1{ width: 500px; height: 200px; font- ...
- Nginx网站常见的跳转配置实例
相信大家在日常运维工作中如果你用到nginx作为前端反向代理服务器的话,你会对nginx的rewrite又爱又恨,爱它是因为你搞定了它,完成了开发人员的跳转需求后你会觉得很爽,觉得真的很强大,恨它是因 ...
- TensorFlow实战12:Bidirectional LSTM Classifier
https://blog.csdn.net/felaim/article/details/70300362 1.双向递归神经网络简介 双向递归神经网络(Bidirectional Recurrent ...