亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

在JavaScript中實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)

 更新時(shí)間:2019年12月24日 14:36:59   作者:Spikef  
這篇文章主要介紹了在JavaScript中實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

鏈?zhǔn)秸{(diào)用實(shí)現(xiàn)本身比較簡(jiǎn)單,也有很多文章詳細(xì)闡述了其實(shí)現(xiàn)方式。本文更多從鏈?zhǔn)秸{(diào)用方法返回值的角度,更進(jìn)一步來(lái)說(shuō)明如何實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用。

什么是鏈?zhǔn)秸{(diào)用

鏈?zhǔn)秸{(diào)用在 JavaScript 語(yǔ)言界很常見(jiàn),如 jQuery 、 Promise 等,都是使用的鏈?zhǔn)秸{(diào)用。鏈?zhǔn)秸{(diào)用可以讓我們?cè)谶M(jìn)行連續(xù)操作時(shí),寫出更簡(jiǎn)潔的代碼。

new Promise((resolve, reject) => {
 resolve();
})
.then(() => {
 throw new Error('Something failed');
})
.then(() => {
 console.log('Do this whatever happened before');
})
.catch(() => {
 console.log('Do that');
})

逐步實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用

假設(shè),我們要實(shí)現(xiàn)一個(gè) math 模塊,使之能夠支持鏈?zhǔn)秸{(diào)用:

const math = require('math');
const a = math.add(2, 4).minus(3).times(2);
const b = math.add(2, 4).times(3).divide(2);
const c = { a, b };

console.log(a.times(2) + b + 1); // 22
console.log(a.times(2) + b + 2); // 23
console.log(JSON.stringify(c)); // {"a":6,"b":9}

基本的鏈?zhǔn)秸{(diào)用

鏈?zhǔn)秸{(diào)用通常的實(shí)現(xiàn)方式,就是在函數(shù)調(diào)用結(jié)果返回模塊本身。那么 math 模塊的代碼大致應(yīng)該是這樣子的:

export default {
 add(...args) {
  // add
  return this;
 },
 minus(...args) {
  // minus
  return this;
 },
 times(...args) {
  // times
  return this;
 },
 divide(...args) {
  // divide
  return this;
 },
}

方法如何返回值

上述代碼實(shí)現(xiàn)了鏈?zhǔn)秸{(diào)用,但是也存在一個(gè)問(wèn)題,就是無(wú)法獲取計(jì)算結(jié)果。所以我們需要對(duì)模塊進(jìn)行改造,使用一個(gè)內(nèi)部變量來(lái)存儲(chǔ)計(jì)算結(jié)果。

export default {
 value: NaN,
 add(...args) {
  this.value = args.reduce((pv, cv) => pv + cv, this.value || 0);
  return this;
 },
}

這樣,我們?cè)谧詈笠徊?,通過(guò) .value 就可以拿到最終的計(jì)算結(jié)果了。

問(wèn)題真的解決了嗎

上面我們看似通過(guò)一個(gè) value 變量解決了存儲(chǔ)計(jì)算結(jié)果的問(wèn)題,但是發(fā)生第二次鏈?zhǔn)秸{(diào)用時(shí), value 的值因?yàn)橐呀?jīng)有了初始值,我們會(huì)得到錯(cuò)誤的計(jì)算結(jié)果!

const a = math.add(5, 6).value; // 11
const b = math.add(5, 7).value; // 23 而非 12

既然是因?yàn)?nbsp;value 有了初始值,那么能不能在獲取 value 的值時(shí)重置掉呢?答案是不能,因?yàn)槲覀儾⒉荒艽_定使用者會(huì)在什么時(shí)候取值。

另外一種思路是在每次鏈?zhǔn)秸{(diào)用之前生成一個(gè)新的實(shí)例,這樣就可以確保實(shí)例之間相互獨(dú)立了。

const math = function() {
 if (!(this instanceof math)) return new math();
};

math.prototype.value = NaN;

math.prototype.add = function(...args) {
 this.value = args.reduce((pv, cv) => pv + cv, this.value || 0);
 return this;
};

const a = math().add(5, 6).value;
const b = math().add(5, 7).value;

但是這樣也不能徹底解決問(wèn)題,假設(shè)我們?nèi)缦抡{(diào)用:

const m = math().add(5, 6);
const c = m.add(5).value; // 16
const d = m.add(5).value; // 21 而非 16

所以,最終要解決這個(gè)問(wèn)題,只能每個(gè)方法都返回一個(gè)新的實(shí)例,這樣可確保無(wú)論怎么調(diào)用,相互之間都不會(huì)被干擾到。

math.prototype.add = function(...args) {
 const instance = math();
 instance.value = args.reduce((pv, cv) => pv + cv, this.value || 0);
 return instance;
};

如何支持不通過(guò) .value 對(duì)結(jié)果進(jìn)行普通運(yùn)算

通過(guò)改造 valueOf 方法或者 Symbol.toPrimitive 方法。其中 Symbol.toPrimitive 方法優(yōu)先 valueOf 方法被調(diào)用,除非是ES環(huán)境不支持。

如何支持 JSON.stringify 序列化計(jì)算結(jié)果

通過(guò)自定義 toJSON 方法。 JSON.stringify 將值轉(zhuǎn)換為相應(yīng)的JSON格式時(shí),如果被轉(zhuǎn)換值有 toJSON 方法,則優(yōu)先使用該方法返回的值。

最終的完整實(shí)現(xiàn)代碼

class Math {
 constructor(value) {
  let hasInitValue = true;
  if (value === undefined) {
   value = NaN;
   hasInitValue = false;
  }
  Object.defineProperties(this, {
   value: {
    enumerable: true,
    value: value,
   },
   hasInitValue: {
    enumerable: false,
    value: hasInitValue,
   },
  });
 }

 add(...args) {
  const init = this.hasInitValue ? this.value : args.shift();
  const value = args.reduce((pv, cv) => pv + cv, init);
  return new Math(value);
 }

 minus(...args) {
  const init = this.hasInitValue ? this.value : args.shift();
  const value = args.reduce((pv, cv) => pv - cv, init);
  return new Math(value);
 }

 times(...args) {
  const init = this.hasInitValue ? this.value : args.shift();
  const value = args.reduce((pv, cv) => pv * cv, init);
  return new Math(value);
 }

 divide(...args) {
  const init = this.hasInitValue ? this.value : args.shift();
  const value = args.reduce((pv, cv) => pv / cv, init);
  return new Math(value);
 }

 toJSON() {
  return this.valueOf();
 }

 toString() {
  return String(this.valueOf());
 }

 valueOf() {
  return this.value;
 }

 [Symbol.toPrimitive](hint) {
  const value = this.value;
  if (hint === 'string') {
   return String(value);
  } else {
   return value;
  }
 }
}

export default new Math();

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論