粥里有勺糖

vuePress-theme-reco 粥里有勺糖    2018 - 2023
粥里有勺糖 粥里有勺糖

Choose mode

  • dark
  • auto
  • light
关于我
备战春秋
  • 心得总结
  • 校招考点汇总
  • 面经汇总
  • 复习自查
技术笔记
  • 技术教程
  • 模板工程
  • 源码学习
  • 技术概念
  • 个人作品
  • 学习笔记
计算机基础
  • 算法与数据结构
  • 操作系统
  • 计算机网络
  • 设计模式
  • 剑指offer
大前端
  • javascript
  • vue
  • html
  • css
  • 🌏浏览器专题
  • Web性能优化
  • regexp
  • node
面试
  • 问解
  • javascript
  • css
  • 手撕代码
  • 性能优化
  • 综合问题
  • 面经汇总
  • 小程序
手撕代码
  • 数据结构与算法
  • javascript
  • css
个人站点
  • GitHub (opens new window)
  • 博客园 (opens new window)
  • 掘金 (opens new window)
线上作品
  • 轻取(文件收集) (opens new window)
  • 个人图床 (opens new window)
  • 考勤小程序 (opens new window)
  • 时光恋人 (opens new window)
  • 在线简历生成 (opens new window)
留言板
Github (opens new window)
author-avatar

粥里有勺糖

285

文章

40

标签

关于我
备战春秋
  • 心得总结
  • 校招考点汇总
  • 面经汇总
  • 复习自查
技术笔记
  • 技术教程
  • 模板工程
  • 源码学习
  • 技术概念
  • 个人作品
  • 学习笔记
计算机基础
  • 算法与数据结构
  • 操作系统
  • 计算机网络
  • 设计模式
  • 剑指offer
大前端
  • javascript
  • vue
  • html
  • css
  • 🌏浏览器专题
  • Web性能优化
  • regexp
  • node
面试
  • 问解
  • javascript
  • css
  • 手撕代码
  • 性能优化
  • 综合问题
  • 面经汇总
  • 小程序
手撕代码
  • 数据结构与算法
  • javascript
  • css
个人站点
  • GitHub (opens new window)
  • 博客园 (opens new window)
  • 掘金 (opens new window)
线上作品
  • 轻取(文件收集) (opens new window)
  • 个人图床 (opens new window)
  • 考勤小程序 (opens new window)
  • 时光恋人 (opens new window)
  • 在线简历生成 (opens new window)
留言板
Github (opens new window)
  • Javascript

    • javscript代码题
    • 简单-实现promiseAll
    • 简单-实现bind
    • 简单-实现call
    • 简单-实现apply
    • 简单-继承实现
    • 简单-new实现
    • 简单-instanceof实现
    • 简单-a同时等于多个值
    • 简单-闭包调用
    • 简单-立即执行的定时器
    • 中等-判断两个日期是否同一周
    • 中等-async/await实现
    • 中等-实现深拷贝
    • 中等-请求合并
    • 中等-数组去重

继承实现

vuePress-theme-reco 粥里有勺糖    2018 - 2023

继承实现

粥里有勺糖 2020-09-05 手撕代码javascript

# 继承实现

# ES6

# class

JS 中并不存在类,class 只是语法糖,本质还是函数

class A{
    constructor(name){
        this.name = name
    }
    printName(){
        console.log(this.name)
    }
}

class B extends A{
    constructor(name,age){
        super(name) // A 的构造函数
        this.age = age
    }
}

let a = new B('123',0)
a.printName() // 123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# ES5

# 组合继承

function A(name) {
    this.name = name
}
A.prototype.printName = function () {
    console.log(this.name)
}

function B(name, age) {
    A.call(this, name)
    this.age = age
}
B.prototype = new A()
B.prototype.constructor = B

let a = new B('123', 0)
a.printName() // 123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 寄生组合继承

function A(name) {
    this.name = name
}
A.prototype.printName = function () {
    console.log(this.name)
}

function B(name, age) {
    A.call(this, name)
    this.age = age
}
B.prototype = Object.create(A.prototype, {
    constructor: {
        value: B,
        enumerable: false,
        writable: true,
        configurable: true
    }
})

let a = new B('123', 0)
a.printName() // 123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function A(name) {
    this.name = name
}
A.prototype.printName = function () {
    console.log(this.name)
}

function B(name, age) {
    A.call(this, name)
    this.age = age
}

function extends(child, parent) {
    const fn = function () { }
    fn.prototype = parent.prototype
    const temp = new fn()
    child.prototype = temp
    temp.constructor = child
}
extends(B, A)
let a = new B('123', 0)
a.printName() // 123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Edit this page (opens new window)
Last Updated: 2022/5/15 12:46:34