hgq's docs
主页
ES6-阮一峰 (opens new window)
Vue文档 (opens new window)
Axios文档 (opens new window)
Vue Router (opens new window)
Vuex文档 (opens new window)
面试题-Vue (opens new window)
面试题-JS (opens new window)

guoguoqiqi

漫不经心的向往
主页
ES6-阮一峰 (opens new window)
Vue文档 (opens new window)
Axios文档 (opens new window)
Vue Router (opens new window)
Vuex文档 (opens new window)
面试题-Vue (opens new window)
面试题-JS (opens new window)
  • JS数据类型
  • JS数据类型的转换
  • 判断数据类型的方法
  • null和undefined的区别
  • parseInt方法解析
  • promise简答
  • var和let以及const的区别
  • 本地存储的几种方式
  • 闭包是什么
  • 调用new的过程
  • 防抖和节流
  • 简单讲讲异步的理解
  • 简单讲讲原型和原型链
  • 判断变量的数据类型
  • 深浅拷贝
  • 数组去重的方法
  • 说一说JS中的this
  • 作用域和作用域链
  • JS的内置对象
  • JS继承的几种实现方式
    • 谈谈你对this、call、apply和bind的理解
    • 说一说原型、原型链
    • 什么是闭包,为什么使用闭包
    • JS中事件冒泡与捕获
    • 说一说JS的事件模型
    • 常用的遍历数组的方法
    • 如何创建一个Ajax
    • js 延迟加载的方式有哪些
    • 谈谈你对模块化开发的理解
    • js 的几种模块规范
    • ES6 模块与 CommonJS 模块、AMD、CMD 的差异
    • JS的运行机制、单线程、事件循环、宏任务微任务
    • arguments 的对象是什么
    • 简单介绍一下 V8 引擎的垃圾回收机制
    • 哪些操作会造成内存泄漏
    • ES6有哪些新特性
    • 说一说箭头函数
    • 什么是高阶函数
    • 为什么函数被称为一等公民
    • js的深浅拷贝
    • 函数柯里化
    • 说一说new操作符
    • 说一说对Promise的理解
    • Generator函数是什么,有什么作用
    • 说一说async和await
    • instanceof的原理是什么,如何实现
    • js 的节流与防抖
    • 相关面试题集合
    • 如何更好的处理async、await的异常
    • JS的事件委托
    • 浏览器和node中事件循环的区别
    • mouseover 和 mouseenter 的区别
    • ===与Object is的区别
    • 数组去重有哪些方法
    • 页面在浏览器中渲染出来的原理和流程
    • js和css是否会阻塞页面的渲染
    • Set、Map、WeakSet 和 WeakMap 的区别
    • 说一说Promise的实现原理
    • 说一说JS的事件循环
    • == 和 === 与隐式转化
    • 说一说回流(重排)和重绘
    • script标签中添加async或defer的作用与区别
    • 如何中断一个请求
    • 遍历一个对象身上属性的方法有哪些
    • 常用的数组方法(不改变原数组)
    • 常用的数组方法(改变原数组)
    • 常用字符串操作方法
    • 对象身上与原型相关的方法
    • 数组的reduce方法
    • 常用的位运算符有哪些
    • 浮点数运算有误差的问题
    • typeof和instanceof的区别
    • 有关js和css对页面渲染阻塞的问题
    • 说说对闭包的理解
    • DOMContentLoaded方法
    • es6中对象新增的方法
    • es6中数组新增的方法
    • es6中字符串新增的方法
    • es6新增的Reflect
    • 如何判断一个变量是否是数组
    • onload 和 DOMContentLoaded的区别
    • 大文件上传问题
    • 上传、下载和普通请求的区别
    • Javascript
    guoguoqiqi
    2022-02-21

    JS继承的几种实现方式

    继承给我们提供了一种优雅可复用的编码方式。

    # 原型链继承

    function Parent () {
        this.name = 'kevin';
    }
    
    Parent.prototype.getName = function () {
        console.log(this.name);
    }
    
    function Child () {
    
    }
    
    Child.prototype = new Parent();
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    问题:

    1. 引用类型的属性被所有实例共享
    2. 创建Child实例的时候无法向Parent传参

    # 借用构造函数(经典继承)

    function Parent () {
        this.names = ['kevin', 'daisy'];
    }
    
    function Child () {
        Parent.call(this);
    }
    
    var child1 = new Child();
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    优点:

    1. 避免了引用类型的属性被所有实例共享
    2. 可以在 Child 中向 Parent 传参

    缺点:

    1. 方法都在构造函数中定义,Child实例无法访问Parent原型上的方法。

    # 组合继承(原型链继承和借用构造函数继承组合的方式)

    function Parent (name) {
        this.name = name;
        this.colors = ['red', 'blue', 'green'];
    }
    
    Parent.prototype.getName = function () {
        console.log(this.name)
    }
    
    function Child (name, age) {
    
        Parent.call(this, name);
    
        this.age = age;
    
    }
    
    Child.prototype = new Parent();
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    优点:

    实现了函数复用,又能保证每个实例都有它自己的属性。

    组合继承避免了原型链和借用构造函数的缺陷,融合了它们的优点,成为 JavaScript 中最常用的继承模式. 而且, instanceof 和 isPrototypeOf( )也能用于识别基于组合继承创建的对象。

    缺点:

    组合继承其实调用了两次父类构造函数, 造成了不必要的消耗。

    # 原型式继承

    function createObj(o) {
        function F(){}
        F.prototype = o;
        return new F();
    }
    
    1
    2
    3
    4
    5

    就是 ES5 Object.create 的模拟实现,将传入的对象作为创建的对象的原型。

    缺点:

    包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承一样。

    # 寄生式继承

    function createAnother(origin){
      var clone=createObj(origin);
      clone.say=function(){
        alert('hi')
      }
      return clone;
    }
    
    1
    2
    3
    4
    5
    6
    7

    寄生式继承是原型式继承的加强版,即在产生了这个继承了父类的对象之后,为这个对象添加一些增强方法。

    # 寄生组合式继承

    function Parent (name) {
        this.name = name;
        this.colors = ['red', 'blue', 'green'];
    }
    
    Parent.prototype.getName = function () {
        console.log(this.name)
    }
    
    function Child (name, age) {
        Parent.call(this, name);
        this.age = age;
    }
    
    // 关键的三步
    var F = function () {};
    
    F.prototype = Parent.prototype;
    
    Child.prototype = new F();
    
    
    var child1 = new Child('kevin', '18');
    
    console.log(child1);
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    这种方式的高效率体现它只调用了一次 Parent 构造函数,并且因此避免了在 Parent.prototype 上面创建不必要的、多余的属性。与此同时,原型链还能保持不变;因此,还能够正常使用 instanceof 和 isPrototypeOf。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。

    # ES6类继承extends

    class Rectangle {
        // constructor
        constructor(height, width) {
            this.height = height;
            this.width = width;
        }
        
        // Getter
        get area() {
            return this.calcArea()
        }
        
        // Method
        calcArea() {
            return this.height * this.width;
        }
    }
    
    const rectangle = new Rectangle(10, 20);
    console.log(rectangle.area);
    // 输出 200
    
    // -----------------------------------------------------------------
    // 继承
    class Square extends Rectangle {
    
      constructor(length) {
        super(length, length);
        
        // 如果子类中存在构造函数,则需要在使用“this”之前首先调用 super()。
        this.name = 'Square';
      }
    
      get area() {
        return this.height * this.width;
      }
    }
    
    const square = new Square(10);
    console.log(square.area);
    // 输出 100
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42

    # 参考文章链接

    JavaScript深入之继承的多种方式和优缺点 (opens new window)
    JS原型链与继承别再被问倒了 (opens new window)
    JavaScript常用八种继承方案 (opens new window)

    ← JS的内置对象 谈谈你对this、call、apply和bind的理解→

    最近更新
    01
    vuex数据持久化怎么做
    05-22
    02
    vue的动态路由怎么配置使用
    05-22
    03
    vue权限控制一般怎么做
    05-22
    更多文章>
    Theme by Vdoing | Copyright © 2022-2022 Guoquoqiqi | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式