仲灏小栈 仲灏小栈
首页
大前端
后端&运维
其他技术
生活
关于我
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

仲灏

诚意, 正心, 格物, 致知
首页
大前端
后端&运维
其他技术
生活
关于我
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 《前端项目基础建设》
  • HTML&CSS

  • JavaScript&TypeScript

  • Node

  • 构建

  • Vue

  • React

  • 小程序

  • 跨端

  • Electron

  • WebGL&GIS

  • 浏览器

  • 面经

  • 其他

  • 大前端
  • 面经
仲灏
2022-06-06
目录

手动实现ES6继承

# 借助构造函数实现

function parent1() {
  this.name = 'parent1'
}

function child1() {
  this.name = 'child1'
	this.age = 18
  
  // 继承this上下文
  parent1.call(this)
}


1
2
3
4
5
6
7
8
9
10
11
12
13

// 原型链继承

function parent2() {
  this.name = 'parent1'
}

function child2() {
  this.name = 'child1'
	this.age = 18
}


child1.prototype = new parent2()
1
2
3
4
5
6
7
8
9
10
11

共享了原型对象地址

组合方式解决


function parent3() {
  this.name = 'parent1'
}

function child3() {
  this.name = 'child1'
	this.age = 18
  
  parent3.call(this)
}

child3.prototype = new parent3()
1
2
3
4
5
6
7
8
9
10
11
12
13

缺点:重复实例化了 而且后面的原型链中方法用不着

child3.prototype = parent3.prototype
1

问题: 会导致实例的__proto__.constructor 指向parent3

child3.prototype = Object.create(parent3.prototype)

上次更新: 2022/07/03, 15:02:21
最近更新
01
vim日常使用记录
04-02
02
滑动窗口最大值
04-02
03
有效的字母异位词
04-02
更多文章>
Theme by Vdoing | Copyright © 2021-2025 izhaong | github | 蜀ICP备2021031194号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式