第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Node.js使用“責(zé)任鏈模式”,但導(dǎo)出后,所有外部調(diào)用都是空的

Node.js使用“責(zé)任鏈模式”,但導(dǎo)出后,所有外部調(diào)用都是空的

尚方寶劍之說 2023-11-11 21:40:22
我試圖用JS實現(xiàn)“責(zé)任鏈模式”,但是我遇到的問題是,導(dǎo)出“chainController”后,所有外部調(diào)用都為NULL。從代碼來看,不存在異步問題,這讓我很困惑,請大家?guī)蛶臀遥浅8兄x代碼:/** Chain **/const Chain = function(handler) {  this.handler = handler;  this.nextHandler = null;}Chain.prototype.setNextHandler = function(nextHandler) {  this.nextHandler = nextHandler;  return nextHandler;}Chain.prototype.pass = function(...args) {  const result = this.handler(...args);  if (result === 'next') {    return this.nextHandler && this.nextHandler.pass(...args);  }  return result;}/** Handlers **/const equalTen = function(number) {  return (number === 10) ? 'equal-ten' : 'next';}const equalTwenty = function(number) {  return (number === 20) ? 'equal-twenty' : 'next';}const equalThirty = function(number) {  return (number === 30) ? 'equal-thirty' : 'next';}/** Controller **/const chainController = function(number) {  const checkEqualTen = new Chain(equalTen);  const checkEqualTwenty = new Chain(equalTwenty);  const checkEqualThirty = new Chain(equalThirty);  checkEqualTen    .setNextHandler(checkEqualTwenty)    .setNextHandler(checkEqualThirty);  return checkEqualTen.pass(number);};//is problem, when external call him, always get nulllet test = chainController(15);console.log(test);
查看完整描述

1 回答

?
PIPIONE

TA貢獻(xiàn)1829條經(jīng)驗 獲得超9個贊

發(fā)生這種情況是因為您從鏈中的最后一個調(diào)用 next (checkEqualThirty()) - 但沒有下一個,因此默認(rèn)答案分配為nextHandler-null因此null返回并 console.logged。


你已經(jīng)沒有給自己留下退路了。我修改了代碼,以便您在 pass 方法中容納其他可能性。


/** Chain **/

const Chain = function(handler) {

  this.handler = handler;

  this.nextHandler = null;

}


Chain.prototype.setNextHandler = function(nextHandler) {

  this.nextHandler = nextHandler;

  return nextHandler;

}


Chain.prototype.pass = function(...args) {

  let result = this.handler(...args);

  if (result === 'next' && this.nextHandler !== null) {

    result = this.nextHandler.pass(...args);

  } else if (result === 'next') {

    result = "Not found";

  }

  return result;

}


/** Handlers **/

const equalTen = function(number) {

  return (number === 10) ? 'equal-ten' : 'next';

}


const equalTwenty = function(number) {

  return (number === 20) ? 'equal-twenty' : 'next';

}


const equalThirty = function(number) {

  return (number === 30) ? 'equal-thirty' : 'next';

}


/** Controller **/

const chainController = function(number) {

  const checkEqualTen = new Chain(equalTen);

  const checkEqualTwenty = new Chain(equalTwenty);

  const checkEqualThirty = new Chain(equalThirty);


  checkEqualTen

    .setNextHandler(checkEqualTwenty)

    .setNextHandler(checkEqualThirty);


  return checkEqualTen.pass(number);

};


//is problem, when external call him, always get null

console.log(chainController(5));

console.log(chainController(10));

console.log(chainController(15));

console.log(chainController(20));

console.log(chainController(25));

console.log(chainController(30));

console.log(chainController(35));


查看完整回答
反對 回復(fù) 2023-11-11
  • 1 回答
  • 0 關(guān)注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號