forked from OleksiyRudenko/a-tiny-JS-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-oop.js
175 lines (159 loc) · 3.64 KB
/
index-oop.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { print } from './js/lib.js';
class Creature {
constructor ({name, gender, legs, phrase}) {
this.name = name;
this.gender = gender;
this.legs = legs;
this.phrase = phrase;
this._friends = [];
this.species = this.constructor.name;
}
set friends(listOfFriends) {
this._friends = listOfFriends;
}
get friends() {
return this._friends.map(({name}) => name).join(', ');
}
toString() {
const {species, name, gender, legs, phrase, friends} = this;
return [species, name, gender, legs, phrase, friends].join('; ');
}
}
class Human extends Creature {
constructor ({name, gender, legs = 2, hands = 2, phrase = 'Hello!'}) {
super({name, gender, legs, phrase});
this.hands = hands;
}
toString() {
const {hands} = this;
const creatureString = super.toString();
const [species, name, gender, legs, phrase, friends] = creatureString.split('; ');
return [species, name, gender, legs, hands, phrase, friends].join('; ');
}
}
class Cat extends Creature {
constructor ({name, gender, legs = 4}) {
const phrase = Cat.getPhrase();
super({name, gender, legs, phrase});
}
toString() {
return super.toString();
}
static getPhrase() {
return 'Meow';
}
}
class Dog extends Creature {
constructor ({name, gender, legs = 4}) {
const phrase = Dog.getPhrase();
super({name, gender, legs, phrase});
}
toString() {
return super.toString();
}
static getPhrase() {
return 'Woof-woof';
}
}
class Catwoman extends Human {
constructor ({name, gender, legs, hands}) {
const phrase = Cat.getPhrase();
super({name, gender, legs, hands, phrase});
}
}
const inhabitantsConfig = [
{
species: Dog,
name: 'Barbos',
gender: 'male',
friends: ['Eve', 'Adam']
},
{
species: Dog,
name: 'Pirate',
gender: 'male',
friends: ['Kain', 'Avan', 'Luna']
},
{
species: Dog,
name: 'Luna',
gender: 'female',
friends: ['Eve', 'Adam', 'Abel', 'Avan', 'Pirate']
},
{
species: Cat,
name: 'Sonya',
gender: 'female',
friends: ['Eve']
},
{
species: Cat,
name: 'Mashka',
gender: 'female',
friends: ['Avan']
},
{
species: Cat,
name: 'Murka',
gender: 'female',
friends: ['Selina']
},
{
species: Human,
name: 'Eve',
gender: 'female',
phrase: 'Hello! Glad to see you!',
friends: ['Barbos', 'Sonya', 'Adam']
},
{
species: Human,
name: 'Lili',
gender: 'female',
phrase: 'Don\'t bother me, please',
friends: []
},
{
species: Human,
name: 'Avan',
gender: 'female',
phrase: 'There are not many people here...',
friends: ['Adam', 'Abel', 'Kain', 'Eve']
},
{
species: Human,
name: 'Adam',
gender: 'male',
phrase: 'Hi! Today is cool!',
friends: ['Barbos', 'Eve']
},
{
species: Human,
name: 'Kain',
gender: 'male',
phrase: 'What\'s up!',
friends: ['Sonya', 'Eve']
},
{
species: Human,
name: 'Abel',
gender: 'male',
phrase: 'How do you do',
friends: ['Selina', 'Eve']
},
{
species: Catwoman,
name: 'Selina',
gender: 'female',
friends: ['Sonya', 'Murka']
}
]
function findInhabitant(name) {
return inhabitants.find(({name: inhabitantName}) => name == inhabitantName);
}
const inhabitants = inhabitantsConfig.map(({species, ...config}) => new species(config));
inhabitantsConfig.forEach(({name, friends}) => {
const currentInhabitant = findInhabitant(name);
const currentFriends = friends.map(friendName => findInhabitant(friendName));
currentInhabitant.friends = currentFriends;
});
inhabitants.forEach(inhabitant => print(inhabitant));