对象组合模式示例

时间:2017-11-25 18:05:02

标签: javascript object-composition

我正在学习JS,并且在JS中遇到了一个关于对象组合模式的有趣article。我在这段代码中想知道的是什么:

let Magic = (superclass) => class extends superclass {  
  shout() {
    if (super.shout) super.shout();
    console.log('Power and wisdom.');
  }  
};
let Fighting = (superclass) => class extends superclass {  
  shout() {
    if (super.shout) super.shout();
    console.log('Strength an courage.');
  }  
};
class Creature {
  constructor(name) {
   this.name = name;  
  }
  shout() {
    console.log(`I'm ${this.name}! Oorah!!`);
  }
};
class DwarfWizard extends Fighting(Magic(Creature)) {  
  courseObjects(object = {}) {
    object.curse = true;
    return object;
  }
}
new DwarfWizard('Thordalf').shout();
// "I'm Thordalf! Oorah!! Power and wisdom. Strength an courage."

courseObjects类中函数DwarfWizard的用途是什么?

  courseObjects(object = {}) {
    object.curse = true;
    return object;
  }

即使我将该功能发表评论,我仍然会得到相同的结果,所以我想知道它的目的是什么?

0 个答案:

没有答案