forked from OleksiyRudenko/a-tiny-JS-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (88 loc) · 2.18 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
96
97
98
99
100
101
102
class Inhabitant {
constructor(name, species, gender, friends, saying) {
this.name = name;
this.species = species;
this.gender = gender;
this.friends = friends;
this.saying = saying;
this.properties = ["name", "species", "gender", "friends", "saying"];
}
toPrint() {
return this.properties
.filter((prop) => {
return Boolean(this[prop]) != false;
})
.map((prop) => `my ${prop}: ` + this[prop])
.join(", ");
}
}
class Person extends Inhabitant {
constructor(name, gender, friends, saying) {
super(name, "human", gender, friends, saying);
this.hands = 2;
this.legs = 2;
}
toPrint() {
return super.toPrint() + `, my hands: ${this.hands}, my legs: ${this.legs}`;
}
}
class Man extends Person {
constructor(name, friends, saying) {
super(name, "man", friends, saying);
}
toPrint() {
return super.toPrint();
}
}
class Woman extends Person {
constructor(name, friends, saying) {
super(name, "woman", friends, saying);
}
toPrint() {
return super.toPrint();
}
}
class Animal extends Inhabitant {
constructor(name, species, paws, gender, friends, saying) {
super(name, species, gender, friends, saying);
this.paws = paws;
}
toPrint() {
return super.toPrint() + `, my paws: ${this.paws}`;
}
}
class Cat extends Animal {
constructor(name, species, paws, gender, friends) {
super(name, species, paws, gender, friends, "meow");
}
toPrint() {
return super.toPrint();
}
}
class Dog extends Animal {
constructor(name, gender, friends) {
super(name, "dog", 4, gender, friends, "woof");
}
toPrint() {
return super.toPrint();
}
}
class Catwoman extends Cat {
constructor(name, paws, gender, hands, friends) {
super(name, "catwoman", paws, gender, friends);
this.hands = hands;
}
toPrint() {
return super.toPrint() + `, my hands: ${this.hands}`;
}
}
const inhabitants = [
new Man("Bob", ["cat"], "hi"),
new Woman("Kate", ["cat", "Bob"]),
new Cat("Tom", "cat", 4, "male"),
new Dog("Jack", "male", ["cat", "Jill"]),
new Catwoman("Jane", 2, "female", 2, ["Ann"]),
];
inhabitants.forEach((inhabitant) => {
print(inhabitant.toPrint());
});