zoukankan      html  css  js  c++  java
  • JS 中的继承

    1. 原型链继承

    function Parent() {
      this.name = 'parent'
      this.play = [1, 2, 3]
    }
    function Child() {
    }
    
    Child.prototype = new Parent()
    const a = new Child()
    const b = new Child()
    console.log(a.play === b.play) //true,因为两个实例通过原型链继承,使用的是同一个原型对象
    

    2. 构造函数继承

    function Parent(age) {
      this.name = 'parent';
      this.age = age;
      this.play = [1, 2, 3];
    }
    
    Parent.prototype.getName = function () {
      return this.name;
    };
    
    function Child(age) {
      Parent.call(this, age);
    }
    
    const a = new Child(20);
    const b = new Child(30);
    console.log(a.play === b.play); // false
    console.log(a.getName()); // 报错,因为构造函数继承只是继承了构造函数里的属性,原型上的属性并未继承
    

    3.组合继承

    function Parent(age) {
      this.name = 'parent';
      this.age = age;
      this.play = [1, 2, 3];
    }
    
    Parent.prototype.getName = function () {
      return this.name;
    };
    
    function Child(age) {
      Parent.call(this, age);
      this.type = 'child';
    }
    
    Child.prototype = new Parent();
    // 手动挂载构造器,指向自己的构造函数
    Child.prototype.constructor = Child;
    const a = new Child(1);
    const b = new Child(10);
    console.log(a instanceof Parent, a.play === b.play); // true,false
    // 缺点:Parent 分别在构造函数上和原型链上都执行了一次,增加了性能开销
    

    4.寄生组合继承

    function Parent() {
      this.name = 'tom';
      this.play = [1, 2, 3];
    }
    
    Parent.prototype.getName = function () {
      return this.name;
    };
    
    function Child() {
      Parent.call(this);
      this.type = 'child';
    }
    Child.prototype = Object.create(Parent.prototype); // 这里使用 Object.create() 节约一次实例化 Parent 的性能开销
    Child.prototype.constructor = Child;
    Child.prototype.getType = function () {
      return this.type;
    };
    
    const a = new Child();
    console.log(a.getName(), a.getType()); // tom child
    
    
  • 相关阅读:
    AWS 磁盘 在线扩容(SSD 磁盘)
    磁盘扩容出错:e2fsck: Bad magic number in super-block while trying to open /dev/vdb1
    manjaro_install_all_in_one
    docker_info_06_stressTest 压力测试
    docker_info_05_registry 仓库管理
    docker_info_04_image 镜像管理
    docker_info_03_volume 数据卷管理
    docker_info_02_network 网络管理
    docker_info_01_install 安装
    docker-ce_install_centos75
  • 原文地址:https://www.cnblogs.com/jiawei-Wang/p/14697502.html
Copyright © 2011-2022 走看看