forked from OleksiyRudenko/a-tiny-JS-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (82 loc) · 2.01 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class Inhabitant {
constructor(species, name, gender, saying) {
this.species = species;
this.name = name;
this.gender = gender;
this.friends = [];
this.saying = saying;
this.props = ["species", "name", "gender", "friends", "saying"];
}
toString() {
return this.props
.map((key) => {
if (key === "friends") {
if (this[key].length === 0) {
return "No friends";
}
return this[key].map((friend) => friend.name).join(", ");
}
return this[key];
})
.join("; ");
}
}
class Animal extends Inhabitant {
constructor(species, name, gender, saying) {
super(species, name, gender, saying);
this.paws = 4;
}
toString() {
return super.toString() + `; ${this.paws} paws`;
}
}
class Dog extends Animal {
constructor(name, gender) {
super("dog", name, gender, "Woof-woof");
}
}
class Cat extends Animal {
constructor(name, gender) {
super("cat", name, gender, "Meoooow");
}
}
class Human extends Inhabitant {
constructor(name, gender, saying) {
super("human", name, gender, saying);
this.legs = 2;
this.hands = 2;
}
toString() {
return super.toString() + `; ${this.legs} legs, ${this.hands} hands`;
}
}
class Woman extends Human {
constructor(name, saying) {
super(name, "female");
this.saying = saying;
}
}
class Man extends Human {
constructor(name, saying, friends = []) {
super(name, "male");
this.saying = saying;
this.friends = friends;
}
}
class CatWoman extends Human {
constructor(catRef) {
super("Selina", "female");
this.cat = catRef;
this.saying = this.cat.saying;
this.species = "catwoman";
}
}
const dog = new Dog("Ronnie", "male");
const cat = new Cat("Reggie", "male");
const woman = new Woman("Agnes", "Hello");
const man = new Man("Mark", "Hi", [cat, dog]);
const catWoman = new CatWoman(cat);
const allInhabitants = [dog, cat, woman, man, catWoman];
for (let inhabitant of allInhabitants) {
print(inhabitant);
}