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

tiny js world oop #378

Merged
merged 3 commits into from
Sep 26, 2022
Merged
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
Prev Previous commit
Next Next commit
review version 2
  • Loading branch information
bmukha committed Sep 23, 2022
commit a99138da25545989aa0e7cb6ecd11af75a4da0f7
79 changes: 48 additions & 31 deletions submissions/bmukha/tiny-js-world-oop/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
*/

class Character {
constructor(specie, name, gender, saying, friends, legs) {
this.specie = specie;
this.name = name;
this.gender = gender;
this.legs = legs;
this.saying = saying;
this.friends = friends;
constructor(parameters) {
({
specie: this.specie,
name: this.name,
gender: this.gender,
legs: this.legs = 2,
saying: this.saying,
friends: this.friends = [],
Copy link
Member

Choose a reason for hiding this comment

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

At the moment of instantiation this refers to not what you think it does.
{species} will extract property species from an object passed as a parameter.
In destructuring you only need to list properties you want to extract, providing default values where needed.
There is a pattern for "renaming" when destructuring, but it is not the case here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@OleksiyRudenko Updated again, please, review.

} = parameters);
}

props() {
return ['specie', 'name', 'gender', 'legs', 'hands', 'saying', 'friends']
getPresentationString() {
return ['specie', 'name', 'gender', 'legs', 'saying', 'friends']
.map((prop) =>
Array.isArray(this[prop]) ? this[prop].join(', ') : this[prop]
)
Expand All @@ -26,48 +28,63 @@ class Character {
}

class Dog extends Character {
constructor(name, gender, saying, friends) {
super('dog', name, gender, saying, friends, 4);
constructor(parameters) {
super({ specie: 'dog', legs: 4, ...parameters });
}
}

const beethoven = new Dog('Beethoven', 'male', 'woof', ['Clyde']);
const beethoven = new Dog({
name: 'Beethoven',
gender: 'male',
saying: 'woof',
friends: ['Clyde'],
});

class Cat extends Character {
constructor(name, gender, saying, friends) {
super('cat', name, gender, saying, friends, 4);
constructor(parameters) {
super({ specie: 'cat', legs: 4, ...parameters });
}
}

const grizabella = new Cat('Grizabella', 'female', 'meow', ['Bonnie']);
const grizabella = new Cat({
name: 'Grizabella',
gender: 'female',
saying: 'meow',
friends: ['Bonnie'],
});

class Human extends Character {
constructor(name, gender, saying, friends) {
super('human', name, gender, saying, friends, 2);
constructor(parameters) {
super({ specie: 'human', ...parameters });
this.hands = 2;
}
getPresentationString() {
return `${super.getPresentationString()}; ${this.hands}`;
}
}

const clyde = new Human('Clyde', 'male', 'Get rich or die trying!', [
'Bonnie',
'Beethoven',
]);
const clyde = new Human({
name: 'Clyde',
gender: 'male',
saying: 'Get rich or die trying!',
friends: ['Bonnie', 'Beethoven'],
});

const bonnie = new Human(
'Bonnie',
'female',
'I have the right to not answer a questions!',
['Clyde', 'Grizabella']
);
const bonnie = new Human({
name: 'Bonnie',
gender: 'female',
saying: 'I have the right to not answer a questions!',
friends: ['Clyde', 'Grizabella'],
});

class CatWomen extends Human {
constructor(name, friends) {
super(name, 'female', grizabella.saying, friends);
constructor(parameters) {
super({ gender: 'female', saying: grizabella.saying, ...parameters });
}
}

const selina = new CatWomen('Selina', []);
const selina = new CatWomen({ name: 'Selina' });

[beethoven, grizabella, clyde, bonnie, selina].forEach((character) =>
print(character.props())
print(character.getPresentationString())
);