第六章(3):繼承

繼承的幾種方式

  • 原型鏈
 /**
   * 原型鏈繼承
   * 本質(zhì)是重寫原型對象
   * 缺點:引用類型值被實例共享
     * @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]

原型鏈?zhǔn)疽鈭D:

  • 構(gòu)造函數(shù)
/**
   * 借用構(gòu)造函數(shù)模式
   * 問題:函數(shù)無法復(fù)用
     * @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"]
  • 組合繼承(將原型鏈和構(gòu)造函數(shù)組合在一起)
/**
   * 組合模式繼承
   * 最常用的模式
     * @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('日暮途遠(yuǎn)', 18);
  let sc1 = new SubCombination('Tiptoe', 20);

  sc.color.push('black');

  console.log(sc.color, sc.getName(), sc.sayAge()) // ["red", "yellow", "black"] "日暮途遠(yuǎn)" 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: '日暮途遠(yuǎn)',
        sayName: function () {
            return this.name;
        },
        game: ['英雄聯(lián)盟', '王者榮耀']
    }

    var o = Object.create(person);
    console.log(o.sayName()); // 日暮途遠(yuǎn)

    o.game.push('戰(zhàn)地')
    console.log(person.game)  // ["英雄聯(lián)盟", "王者榮耀", "戰(zhàn)地"]
  • 寄生式組合繼承
/**
   * 寄生組合式繼承
   * 優(yōu)點:避免2次調(diào)用父類構(gòu)造函數(shù)
     * @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('日暮途遠(yuǎn)', 18)
  console.log(sI.sayName()) // 日暮途遠(yuǎn)

引用

javascript高級程序設(shè)計第三版

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 工廠模式類似于現(xiàn)實生活中的工廠可以產(chǎn)生大量相似的商品,去做同樣的事情,實現(xiàn)同樣的效果;這時候需要使用工廠模式。簡單...
    舟漁行舟閱讀 8,140評論 2 17
  • 單例模式 適用場景:可能會在場景中使用到對象,但只有一個實例,加載時并不主動創(chuàng)建,需要時才創(chuàng)建 最常見的單例模式,...
    Obeing閱讀 2,321評論 1 10
  • javascript有很多創(chuàng)建對象的模式,完成工作的方式也不只一種。你可以隨時定義自己的類型或自己的泛用對象。可以...
    WanLum閱讀 294評論 0 0
  • var a = 1; console.log(typeof a);// 'number' var b = '1';...
    zdnexus閱讀 420評論 0 0
  • 我的思念是圓的 八月中秋的月亮 也是最亮最圓的 無論山多高,海多寬 天涯海角都能看見它 在這樣的夜晚 會想起什么?...
    矗立在西北的白楊樹閱讀 1,824評論 0 1

友情鏈接更多精彩內(nèi)容