多态性

function ask(question, ...handlers) {
  let isYes = confirm(question);

  for(let handler of handlers) {
    if (handler.length == 0) {
      if (isYes) handler();
    } else {
      handler(isYes);
    }
  }

}

// 对于积极的回答,两个 handler 都会被调用
// 对于负面的回答,只有第二个 handler 被调用
ask("Question?", () => alert('You said yes'), result => alert(result));

 

posted @ 2020-04-26 20:42  LangZ-  阅读(148)  评论(0编辑  收藏  举报