Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OOP exercise #682

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
76 changes: 76 additions & 0 deletions submissions/Dovahkiin1991/oop-exercise/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { print } from './js/lib.js';
/* Refer to https://github.com/OleksiyRudenko/a-tiny-JS-world for the task details
Complete the below for code reviewers' convenience:

Code repository: https://github.com/Dovahkiin1991/a-tiny-JS-world
Web app: https://dovahkiin1991.github.io/a-tiny-JS-world/
*/
// ======== OUTPUT ========
const UNSET_VALUE = 'unset';
class Creature {
constructor(species, name, gender, legs, hands, saying) {
this.species = species;
this.name = name;
this.gender = gender;
this.legs = legs;
this.hands = hands;
Copy link
Member

Choose a reason for hiding this comment

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

Hands are inherited by all animals. They just do not need them.
OOP helps to define properties exactly where they needed. OOP in general and class syntax in particular is more powerful thing than just a tool to define default values for numerous properties.

this.saying = saying;
}

listOfProperties() {
Dovahkiin1991 marked this conversation as resolved.
Show resolved Hide resolved
return `
species - ${this.species = this.species || UNSET_VALUE};
name - ${this.name = this.name || UNSET_VALUE};
gender - ${this.gender = this.gender || UNSET_VALUE};
legs - ${this.legs = this.legs || UNSET_VALUE};
hands - ${this.hands = this.hands || UNSET_VALUE};
Dovahkiin1991 marked this conversation as resolved.
Show resolved Hide resolved
saying - ${this.saying = this.saying || UNSET_VALUE};
Dovahkiin1991 marked this conversation as resolved.
Show resolved Hide resolved
`;
}
}

class Human extends Creature {
constructor(species, name, gender, legs, hands) {
super(species, name, gender, legs, hands, `Hey, my name is ${name}!`)
}
}

class Man extends Human {
constructor(name, gender, legs, hands, saying) {
super('man', name, gender, legs, hands, saying)
}
}

class Woman extends Human {
constructor(name, gender, legs, hands, saying) {
super('woman', name, gender, legs, hands, saying)
Dovahkiin1991 marked this conversation as resolved.
Show resolved Hide resolved
}
}

class Animal extends Creature {
constructor(species, name, gender, legs, hands, saying) {
super(species, name, gender, legs, hands, saying)
}
}

class Cat extends Animal {
constructor(name, gender, legs) {
super('cat', name, gender, legs, '', 'meuw-meuw!');
}
}
class Dog extends Animal {
constructor(name, gender, legs) {
super('dog', name, gender, legs, '', 'woof-woof!');
}
}

const inhabitantsList = [
new Man('Alex', 'male', 2, 2),
new Woman('Katie', 'female', 2, 2),
new Cat('Simba', 'female', 4),
new Dog('Toby', 'male', 4),
];

inhabitantsList.map((inhabitant) => {
print(inhabitant.listOfProperties(), 'div');
});