我的网站开发技术经验总结 我的网站开发技术经验总结
首页

fangdown

我的网站开发技术经验总结
首页
  • 大前端

  • 基础

    • js

      • 你知道new操作符是如何实现的吗
      • 说说大文件上传的思路
      • 说说对闭包的理解及应用
      • 说说对单点登录sso的理解
      • 说说对防抖节流的理解
      • 说说对函数式编程的理解
      • 说说对内存泄漏的理解及触发场景
      • 说说对事件模型的理解
      • 说说对事件委托的理解及应用场景
      • 说说对原型及原型链的理解
      • 说说对正则表达式的理解
      • 说说对BOM的理解及常见操作
      • 说说对Dom的理解及常见操作
      • 说说对JavaScript中事件循环的理解
      • 说说对js的执行上下文的理解
      • 说说对js中变量作用域的理解
      • 说说对js中继承的理解及实现方式
        • 是什么
        • 实现方式
          • 原型链继承
          • 构造函数继承
          • 寄生组合继承
          • class
      • 说说对this关键字的理解
      • 说说你对 Immutable Data的理解?如何应用在React项目中
      • 说说如何判断数据类型
      • 说说如何判断一个元素在可视区域内
      • 说说如何实现函数缓存
      • 说说深浅拷贝的区别及实现
      • 说说什么是尾递归及其应用
      • 说说为什么0.1+0.2!==0.3
      • 说说下拉刷新,上拉加载的原理
      • 说说ajax的实现原理
      • 说说call-apply-bind的作用及区别
      • 说说js中本地存储有哪些方式及区别
      • 说说js中的类型转换机制
      • Javscript数组的常用方法有哪些?
      • Javscript字符串的常用方法有哪些?
    • css

    • ES6

  • 框架

  • 情商

  • 算法

  • 网络

  • 千锤百炼
  • 基础
  • js
fangdown
2021-08-31
目录

说说对js中继承的理解及实现方式

# 是什么

继承:子类具有父类的各种属性和方法,不需要再写相同的编码

class Car {
  constructor(color, speed){
    this.color = color
    this.speed = speed
  }
}
class Tk extends Car{
  constructor(color, speed, size){
    // 必须要super
    super(color, speed)
    this.size  = size
  }
}
const tk = new Tk('red', '100', 'small')
console.log(tk.size) //small
console.log(tk.color) // red

# 实现方式

  • 原型链继承: 子原型对象等于某个对象, 缺点:实例共享
  • 构造函数继承: call的方式获取, 缺点:不继承原型链
  • 寄生组合继承: 结合原型链和构造函数继承

# 原型链继承

function Parent(){
  this.address = '腾大1号'
  this.data = []
}
Parent.prototype.getData = function(){
  console.log('hello parent')
}
function Child(){
  this.type = 'sub'
}
Child.prototype = new Parent()
const c1 = new Child()
const c2  = new Child()
c1.data.push(1)
console.log(c1.data) // [1]
console.log(c2.data) // [1]
c1.getData() // hello parent

# 构造函数继承

function Parent(address){
  this.address = address
  this.data = []
}
Parent.prototype.getData = function(){
  console.log('hello parent')
}
function Child(address){
  Parent.call(this, address)
  this.type = 'child'
}

const c11 = new Child('南山')
const c12 = new Child('龙岗')
c11.data.push(111)
console.log(c11.address) // 南山
console.log(c12.address) // 龙岗
console.log(c11.data) // [111]
console.log(c12.data) // [] 不会污染
c11.getData() // 报错,不会继承原型链

# 寄生组合继承

function Parent() {
  this.data = [];
}
Parent.prototype.showName = function() {
  console.log(this.name);
};
Parent.prototype.showData = function() {
  console.log(this.data);
};
function Child(name) {
  Parent.call(this);
  this.name = name;
}

function clonePrototype(parent, child) {
  // Object.create 就可以减少组合继承中多进行一次构造的过程
  child.prototype = Object.create(parent.prototype);
  child.prototype.constructor = child;
}
clonePrototype(Parent, Child);
const c1 = new Child("c1");
const c2 = new Child("c2");
c1.data.push(5555);
c2.data.push(3333);
c1.showData(); // 实例不共享
c2.showData(); // 原型链可使用
c1.showName();
c2.showName();
// [ 5555 ]
// [ 3333 ]
// c1
// c2

# class

extends + super

class P {
  constructor(name){
    this.data = []
    this.name = name
  }
  // 原型方法
  showData(){
    console.log(this.data)
  }
  showName(){
    console.log(this.name)
  }
}
class C extends P{
  constructor(name){
    super(name)
  }
}
const cc1 = new C('cc1')
const cc2 = new C('cc2')
cc1.data.push('cc1')
cc2.data.push('cc2')
cc1.showData()
cc2.showData()
cc1.showName()
cc2.showName()

// [ 'cc1' ]
// [ 'cc2' ]
// cc1
// cc2
#js
上次更新: 2021/12/19, 18:05:42
说说对js中变量作用域的理解
说说对this关键字的理解

← 说说对js中变量作用域的理解 说说对this关键字的理解→

最近更新
01
多分支修复撞车的问题
05-01
02
如何成为架构师
01-23
03
服务器部署全过程
11-23
更多文章>
Theme by Vdoing | Copyright © 2019-2026 fangdown | 粤ICP备19079809号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式