溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

nodejs中怎么定義和繼承類

發(fā)布時(shí)間:2021-07-21 10:39:55 來源:億速云 閱讀:130 作者:Leah 欄目:web開發(fā)

nodejs中怎么定義和繼承類,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

套路1. 在構(gòu)造函數(shù)(constructor)中總是使用instanceof操作符:

function Base() {
  if (!(this instanceof Base)) {
    return new Base();
  }
}

上述代碼的含義就是: 如果Base這個(gè)函數(shù)調(diào)用時(shí)沒有使用new操作符,則會(huì)自動(dòng)調(diào)用new操作符,返回Base的實(shí)例

套路2. 所有成員變量定義在構(gòu)造函數(shù)(constructor)中

function Base() {
  if (!(this instanceof Base)) {
    return new Base();
  }

  //開始成員變量定義
  this.className = "Base";
}

套路3. 所有的成員方法以函數(shù)表達(dá)式方式定義在原型(prototype)中【為什么要這樣,其原因在套路4中的inherits源碼注釋中】

Base.prototype.printClassName = function(){
   console.log(this.className);
}

調(diào)用如下:

var base = Base(); //不使用new操作符,直接進(jìn)行函數(shù)調(diào)用,自動(dòng)調(diào)用new操作符
console.log(base.className);
base.printClassName();

套路4. 使用util.inherits(子類,父類)進(jìn)行原型(prototype)繼承

先來看一下inherits的源碼:

var inherits = function(ctor, superCtor) {
  //嚴(yán)格相等測試:undefined/null
  //子類構(gòu)造函數(shù)必須存在
  if (ctor === undefined || ctor === null)
    throw new TypeError('The constructor to "inherits" must not be ' +
      'null or undefined');
  //嚴(yán)格相等測試:undefined/null
  //父類構(gòu)造函數(shù)必須存在
  if (superCtor === undefined || superCtor === null)
    throw new TypeError('The super constructor to "inherits" must not ' +
      'be null or undefined');

  //要點(diǎn): 如果要繼承的話,父類必須要有prototype對象
  //這也是為什么將所有成員方法都定義在prototype對象中?。?!
  if (superCtor.prototype === undefined)
    throw new TypeError('The super constructor to "inherits" must ' +
      'have a prototype');

  //讓子類構(gòu)造函數(shù)對象增加一個(gè)super_指針,指向父類,這樣就形成繼承鏈
  ctor.super_ = superCtor;

  //調(diào)用Object.setPrototypeOf(子類的prototype,父類的prototype)
  Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
};

Object.setPrototypeOf : 該鏈接可以了解一下setPrototypeOf方法,非常簡單,其Polyfill如下:

// 僅適用于Chrome和FireFox,在IE中不工作:
Object.setPrototypeOf = Object.setPrototypeOf || function (obj, proto) {
 obj.__proto__ = proto;
 return obj; 
}

我們來測試一下繼承。

先定義子類

function Child() {
  //老樣子,套路1
  if (!(this instanceof Child)) {
    return new Child();
  }
}

然后根據(jù)套路4, 調(diào)用inherits函數(shù)進(jìn)行原型繼承

//注意,inherits調(diào)用不在構(gòu)造函數(shù),也不在原型對象,而是全局調(diào)用
inherits(Child, Base);

最后我們調(diào)用一下child的printClassName方法,該方法在基類原型對象中實(shí)現(xiàn)。

nodejs中怎么定義和繼承類

子類調(diào)用基類函數(shù)-undefined.png

出現(xiàn)錯(cuò)誤,child.printClassName()后輸出undefined!

為什么呢?

套路5. 子類的構(gòu)造函數(shù)中使用 父類.call(this),實(shí)現(xiàn)父類構(gòu)造函數(shù)中的成員變量繼承

function Child() {
  //老樣子,套路1
  if (!(this instanceof Child)) {
    return new Child();
  }

  //增加這句話,在調(diào)用printClassName就能正常的輸出Base字符串
  Base.call(this);

  //如果要更新基類的成員變量,請?jiān)贐ase.call(this)之后!
  this._className = "Child"; //調(diào)用printClassName就能正常的輸出Child字符串
}

Function.prototype.call()

由此可見,nodejs中的繼承需要:

在構(gòu)造函數(shù)中調(diào)用 父類.call(this),實(shí)現(xiàn)父類成員變量的繼承

全局調(diào)用inherits(子類,父類) 進(jìn)行父類成員函數(shù)的繼承

關(guān)于nodejs中怎么定義和繼承類問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI