0

I am learning JS and have came across an interesting article about object composition pattern in JS. What I am wondering in this code:

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."

What is the purpose of the function courseObjects in the DwarfWizard class?

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

I still get the same result even when I comment out the function, so I am wondering what is it's purpose?

Leff
  • 1,968
  • 24
  • 97
  • 201
  • 1
    That function isn’t called anywhere, so it makes sense that it doesn’t make any difference. – Sebastian Simon Nov 25 '17 at 18:07
  • Yep, it is merely an example. It makes no difference if it is present or not. – acdcjunior Nov 25 '17 at 18:15
  • It has nothing to do with JS, it is a function in the theme of the example. What it will do is receive an object and will add the property "curse" to it and will assign the value "true" to the object. So if this is a code example of a game the dwarf wizard can apply this method to a sword, for example, and then the sword in addition to probably other property like weight, power and so on will have curse as well. Maybe later in the game a check if an object is cursed is happening the determine some kind of outcome. – DSCH Nov 25 '17 at 18:15
  • `curseObjects` is the ability that a `DwarfWizard` has that is unique to it, it could have been anything. He picked it as an example. – Jared Smith Nov 25 '17 at 18:15
  • That messy code shouldn't be applied anywhere. Furthermore, as you are learning, you should steer clear from that kinda of code examples. There are far better and often used ways of working with "classes" in js. If you are not using syntactically suggar snippets, you can always use prototype. – Andy Nov 25 '17 at 18:41
  • It seems to curse the object that is passed as the argument. – Bergi Nov 25 '17 at 20:03

0 Answers0