Skip to content
Merged
Changes from 3 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
96 changes: 96 additions & 0 deletions submissions/asaMitaka/OOP exercise/indexClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Main Class */
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, hands, wings, saying) {
this.species = 'mammal';
this.name = name;
this.gender = gender;
this.legs = legs;
this.hands = hands;
this.wings = wings;
this.saying = saying;
}

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

/* Human */
Copy link

Choose a reason for hiding this comment

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

Is there really sense in these comments? Human class is called Human already. Same for others.

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

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

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

class Woman extends Human {
constructor (name, saying = 'hello', species, legs, hands) {
super(species, legs, hands);
this.name = name;
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.

}
}

/* Dog */
class Dog extends Mammal {
constructor (name, gender, saying = 'Woof-Woof', ) {
super();
this.species = 'dog';
this.name = name;
this.gender = gender;
this.legs = 4;
this.hands = 0;
this.saying = saying;
}

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

Choose a reason for hiding this comment

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

Instead of rewriting the method, you can use the Mammal's aboutMe method in conjunction with a new one. You can call it from super. You just need to change it a little so that properties don't repeat.

}
}

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

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

let john = new Man('John', 'Hello');
Copy link

Choose a reason for hiding this comment

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

These are consts.

let cassy = new Woman('Cassy', 'Hi!');
let ralph = new Dog('Ralph', 'male', 'Woooooof');
let aimi = new Cat('Aimi', 'female', 'meeeeeeoooooooow');

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

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