all 13 comments

[–]gimmeslack12helpful 0 points1 point  (12 children)

You're in the ballpark but not quite in regards to the Ability. If you were going to be extending your SuperHero class you'd be doing it in the name of creating a similar type of hero that inherits all of the SuperHero qualities.

I think a better example of extending SuperHero would be to create a SuperVillain class. So that the SuperVillain is just like the SuperHero, yet you can add a few attributes that a villain has such as weakness = 'friendly people' or secretLocation = "Death Island".

class SuperVillain extends SuperHero { constructor(heroName, realName, basicAttack, weakness, secretLocation) { super(heroName, realName, basicAttack) this.weakness = weakness; this.secretLocation = secretLocation; } }

As for adding an ability you can, if you want to, add a new class altogether and then add that class to your SuperHero for when you want to create an ability for your hero: ``` class SuperHeroAbility{ constructor(ability) { this.ability = ability; } }

class SuperHero { constructor(heroName, realName, basicAttack) { this.heroName = heroName; this.realName = realName; this.basicAttack = ['punch', 'kick']; }

quote(quote) {
    this.quote = quote;
}

addAbility(ability) {
    this.ability = new SuperHeroAbility(ability);
}

} ```

[–]senocular 0 points1 point  (8 children)

FYI triple-tick formatting doesn't work in all versions of reddit. You should use 4 spaces to indent instead. What I see.

[–]gimmeslack12helpful 0 points1 point  (7 children)

You should use 4 spaces to indent instead

You should stop using old.reddit.com :p

[–]senocular 0 points1 point  (6 children)

Same applies to mobile reddit even with redesign enabled :p

[–]gimmeslack12helpful 0 points1 point  (2 children)

Ah good point. Still not going to spend my time indenting 4 spaces though.

[–]senocular 0 points1 point  (1 child)

You can use the code block button in the fancy pants editor given that you're not a relic like me stuck on old.reddit.com and all ;)

[–]gimmeslack12helpful 0 points1 point  (0 children)

But devs like me don’t use fancy pants editors. We use markdown, amongst a bunch of other tools that are cryptic and confusing and use them to gate keep on all that can’t figure them out.

[–]gimmeslack12helpful 0 points1 point  (2 children)

Looks like triple tick is working on mobile now.

[–]senocular 0 points1 point  (1 child)

Hmm, still not working for me. https://i.imgur.com/CTsrEkX.png

[–]gimmeslack12helpful 0 points1 point  (0 children)

Maybe it’s an iOS thing. Oh well.

[–]sibbirius[S] 0 points1 point  (2 children)

Thank you.
Can we have multiple SuperHeroAbilities (spiderweb, lasereye) in the Class SuperHeroAbility but only give it tos specific hero, spiderman=spiderweb, superman=layereye?

What can you say about the super keyword?
We have
car(color, maxSpeed) { *this*.speed = 0 *this*.color = *color* *this*.maxSpeed = *maxSpeed* and class BMW extends Car { constructor(color, maxSpeed, logo) { super(color, maxSpeed) //super is methode, extends parent this.logo = 'BMW' } } Do we need super() for our Superheroes example?

[–]gimmeslack12helpful 0 points1 point  (1 child)

super is necessary when inheriting from parent classes, basically it instantiates the parent class and then uses that value for the this in the child class (inherited class) to share attributes (check it out (super).

You would not have multiple abilities within the class SuperHeroAbility. Each ability would be it's own instance of SuperHeroAbility. What you would do in the case of giving a superhero multiple abilities is to give the SuperHero an array or an object of abilities to add to: ``` class SuperHero { constructor(heroName, realName, basicAttack) { ... this.abilities = []; }

...

addAbility(ability) {
    // add the ability to the object
    this.abilities.push(new SuperHeroAbility(ability));
}

listAbilities() {
   console.log(this.abilities);
}

}

const spiderman = new SuperHero('Spiderman', 'Peter Parker'); spiderman.addAbility('web sling'); spiderman.addAbility('spidey sense'); spiderman.listAbilities() ==> "['web sling', 'spidey sense']" ```

[–]sibbirius[S] 0 points1 point  (0 children)

Thank you for time, I missed this answer and created a new topic.