Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions submissions/asaMitaka/OOP exercise/indexClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class Mammal {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mammal class should store values that are exclusive to it. Does every mammal have hands or wings?

constructor (name, gender, legs, saying) {
this.species = 'mammal';
this.name = name;
this.gender = gender;
this.legs = legs;
this.saying = saying;
}

tellAboutClass() {
return `${this.saying} My name is ${this.name}. I'm ${this.species} ${this.gender}. I have ${this.legs} legs.`;
}
}

class Human extends Mammal{
constructor (name, gender, saying) {
super(name, gender);
this.species = 'human';
this.legs = 2;
this.hands = 2;
this.saying = saying;
}

tellAboutClass() {
return `${super.tellAboutClass()} I have ${this.hands} hands.`;
}
}

class Man extends Human {
constructor (name, saying = 'Hello.') {
super(name, saying);
this.gender = 'male';
this.saying = saying;
}
}

class Woman extends Human {
constructor (name, saying = 'Hi') {
super(name, saying);
this.gender = 'female';
this.saying = saying;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of repeating actions, for example - saying. When you create Woman, you pass a string as a saying to it. It gets it as a constructor parameter and then passes it to the super constructor. Later, on the 41 line it assigns this value to the this. saying property. But what does super do with it? Well, here, basically there's broken logic since the second parameter of the Human constructor is gender. So you pass saying as gender. And you also rewrite gender on line 40. Do you see what I'm saying? Please restructure your code, maybe even from scratch to make sure that every class is responsible for its exclusive properties and if something needs to be set from outside (like names or genders) it is passed properly.

}
}

class Dog extends Mammal {
constructor (name, gender, saying = 'woof') {
super(name, gender);
this.species = 'dog';
this.legs = 4;
this.saying = saying;
}
}

class Cat extends Mammal {
constructor (name, gender, saying = 'Meow') {
super(name, gender);
this.species = 'cat';
this.legs = 4;
this.saying = saying;
}
}

const john = new Man('John');
const cassy = new Woman('Cassy', 'Hi!');
const ralph = new Dog('Ralph', 'male', 'Woooooof');
const aimi = new Cat('Aimi', 'female', 'Meeeeeeoooooooow');

const inhabitants = [john, cassy, ralph, aimi];

inhabitants.forEach( item => print(item.tellAboutClass()));