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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

我試圖用JS實(shí)現(xiàn)“責(zé)任鏈模式”,但是我遇到的問(wèn)題是,導(dǎo)出“chainController”后,所有外部調(diào)用都為NULL。從代碼來(lái)看,不存在異步問(wèn)題,這讓我很困惑,請(qǐng)大家?guī)蛶臀?,非常感謝代碼:/** 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)驗(yàn) 獲得超9個(gè)贊

發(fā)生這種情況是因?yàn)槟鷱逆溨械淖詈笠粋€(gè)調(diào)用 next (checkEqualThirty()) - 但沒(méi)有下一個(gè),因此默認(rèn)答案分配為nextHandler-null因此null返回并 console.logged。


你已經(jīng)沒(méi)有給自己留下退路了。我修改了代碼,以便您在 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));


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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