第六章(3):继承

作者: 日暮途远_ | 来源:发表于2017-07-25 18:40 被阅读20次

继承的几种方式

  • 原型链
 /**
   * 原型链继承
   * 本质是重写原型对象
   * 缺点:引用类型值被实例共享
     * @constructor
     */
  function Super() {
    this.property = 'super'
    this.arr = [1,2]
  }

  Super.prototype.getValue = function () {
    return this.property
  }

  function Sub() {

  }

  Sub.prototype = new Super();

  let sub = new Sub()
  console.log(sub.getValue())  // super

  console.log(Super.prototype.isPrototypeOf(sub)) // true
  console.log(Sub.prototype.isPrototypeOf(sub)) // true
  console.log(Object.prototype.isPrototypeOf(sub)) // true

  let sub1 = new Sub()
  sub1.arr.push(3);

  let sub2 = new Sub()
  console.log(sub1.arr) // [1,2,3]
  console.log(sub2.arr) // [1,2,3]

原型链示意图:

  • 构造函数
/**
   * 借用构造函数模式
   * 问题:函数无法复用
     * @constructor
     */
    function SuperInstance() {
    this.color = ['red', 'yellow']
  }

  function SubInstance() {
    SuperInstance.call(this)
  }

  var si = new SubInstance();
    var si1 = new SubInstance();

    si.color.push("black");
    console.log(si.color) //  ["red", "yellow", "black"]
    console.log(si1.color)  //  ["red", "yellow"]
  • 组合继承(将原型链和构造函数组合在一起)
/**
   * 组合模式继承
   * 最常用的模式
     * @param name
     * @constructor
     */
  function SuperCombination(name) {
        this.name = name;
    this.color = ['red', 'yellow'];
  }

  SuperCombination.prototype.getName = function () {
    return this.name;
  }

  function SubCombination(name, age) {
    SuperCombination.call(this, name)
    this.age = age
  }

  SubCombination.prototype = new SuperCombination();
//  SubCombination.prototype.constructor = SubCombination;

  SubCombination.prototype.sayAge = function () {
    return this.age;
  }

  let sc = new SubCombination('日暮途远', 18);
  let sc1 = new SubCombination('Tiptoe', 20);

  sc.color.push('black');

  console.log(sc.color, sc.getName(), sc.sayAge()) // ["red", "yellow", "black"] "日暮途远" 18
  console.log(sc1.color, sc1.getName(), sc1.sayAge()) //  ["red", "yellow"] "Tiptoe" 20
  • 原型式继承
    /**
     * 原型式继承
     * 缺点: 引用类型共享
     * @type {{name: string, sayName: person.sayName, game: [string,string]}}
     */
    var person = {
        name: '日暮途远',
        sayName: function () {
            return this.name;
        },
        game: ['英雄联盟', '王者荣耀']
    }

    var o = Object.create(person);
    console.log(o.sayName()); // 日暮途远

    o.game.push('战地')
    console.log(person.game)  // ["英雄联盟", "王者荣耀", "战地"]
  • 寄生式组合继承
/**
   * 寄生组合式继承
   * 优点:避免2次调用父类构造函数
     * @param subType
     * @param superType
     */
  function inherit(subType, superType) {
    var property = Object.create(superType.prototype);
    property.constructor = subType;
    subType.prototype = property;
  }

  function SuperIn(name) {
    this.name = name
  }

  SuperIn.prototype.sayName = function () {
    return this.name;
  }

  function SubIn(name, age) {
    SuperIn.call(this, name)
    this.age = age
  }

  inherit(SubIn, SuperIn)

  SubIn.prototype.sayAge = function () {
    return this.age;
  }

  let sI = new SubIn('日暮途远', 18)
  console.log(sI.sayName()) // 日暮途远

引用

javascript高级程序设计第三版

相关文章

  • 第六章(3):继承

    继承的几种方式 原型链 原型链示意图: 构造函数 组合继承(将原型链和构造函数组合在一起) 原型式继承 寄生式组合...

  • 遗产继承知识(六)

    第六章 女儿与父异母同姐妹的继承 问:亡人只有女儿时,怎么继承? 答:有三种继承方式: 1、若亡人只有一个女儿时...

  • js面向对象_继承

    1、//call继承 } 2、//原型继承 3、//组合继承 4、//冒充继承

  • 3、继承

    1.什么是继承 一个类继承另外一个类,其中会产生继承者和被继承者。这儿的继承者叫子类,被继承者叫父类/超类继承就是...

  • Cpp3 继承

    Cpp3 继承 什么是继承 总结: 1、什么是继承?继承就是数据的复制2、为什么要用继承?减少重复代码的编写3、P...

  • 《岳响河》目录 第六章

    第六章1 第六章2 第六章3 第六章4 第六章5 第六章6 第六章7 第六章8 第六章9 第六章10 第六章11 ...

  • es5的部分继承以及es6的class

    一、JavaScript常用的原型继承方式 原型链继承 2,构造函数继承(对象冒充继承) 3,组合继承(原型链继承...

  • JS中继承的方式

    讨论三种常用的继承方式: 组合继承 原型新对象继承 3 . 寄生继承

  • JavaScript 继承 3 组合继承

    组合继承,有时候也叫做伪经典继承,指的是将原型链和借用构造函数的技术组合到一起,从而发挥二者之长的一种继承模式。其...

  • Geekband-job3-note

    1、继承 C++类的3种继承方式,分别是public继承,protected继承,private继承。最常用的还是...

网友评论

    本文标题:第六章(3):继承

    本文链接:https://www.haomeiwen.com/subject/kvlgkxtx.html