forked from OleksiyRudenko/a-tiny-JS-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (57 loc) · 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* Refer to https://github.com/OleksiyRudenko/a-tiny-JS-world for the task details
Complete the below for code reviewers' convenience:
Code repository: _put repo URL here_
Web app: _put project's github pages URL here_
*/
// ======== OBJECTS DEFINITIONS ========
class Inhabitant {
constructor(nature, gender, name, says, legs) {
this.nature = nature;
this.gender = gender;
this.name = name;
this.says = says;
this.legs = legs;
this.friends = [];
}
addFriend(...friendObjects) {
this.friends = this.friends.concat(friendObjects);
}
toString() {
return [
`name: ${this.name}`,
`nature: ${this.nature}`,
`says: ${this.says}`,
`legs: ${this.legs}`,
`friends: ${this.friends.map(e => e.name).join(', ')}`,
`gender: ${this.gender}`
].join('; ');
}
};
class Human extends Inhabitant {
constructor(nature, gender, name, says, legs = 2, hands = 2) {
super(nature, gender, name, says, legs);
this.hands = hands;
}
toString() {
return super.toString() + `; hands: ${this.hands};`;
}
};
class Cat extends Inhabitant {
constructor(nature, gender, name, says, legs = 4) {
super(nature, gender, name, says, legs);
}
};
class Dog extends Inhabitant {
constructor(nature, gender, name, says, legs = 4) {
super(nature, gender, name, says, legs)
}
};
const cat = new Cat('cat', 'female', 'Jessy', 'Wanna eat!');
const dog = new Dog('dog', 'male', 'Kim', 'Awa-waw!');
const woman = new Human('woman', 'female', 'Kate', 'I need hugs!');
const man = new Human('man', 'male', 'Justin', 'I love comics.');
cat.addFriend(man);
dog.addFriend(woman, man);
man.addFriend(woman, dog, cat);
woman.addFriend(man, dog);
[cat, dog, man, woman].forEach(printElement);