-
Notifications
You must be signed in to change notification settings - Fork 1
/
7-2-Person.js
81 lines (70 loc) Β· 1.43 KB
/
7-2-Person.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
/**
* p248 μμ
*/
class Person {
#name;
#courses;
constructor(name) {
this.#name = name;
this.#courses = [];
}
get name() {
return this.#name;
}
get courses() {
return this.#courses.slice();
}
set courses(aList) {
this.#courses = aList.slice();
}
addCourse(aCourse) {
this.#courses.push(aCourse);
}
removeCourse(
aCourse,
fnIfAbsent = () => {
throw new RangeError();
}
) {
const index = this.#courses.indexOf(aCourse);
if (index === -1) fnIfAbsent();
else this.#courses.splice(index, 1);
}
}
class Course {
#name;
#isAdvanced;
constructor(name, isAdvanced) {
this.#name = name;
this.#isAdvanced = isAdvanced;
}
get name() {
return this.#name;
}
get isAdvanced() {
return this.#isAdvanced;
}
}
/**
* μμ μ€νμ μν μμμ μ½λ
*/
const filename = 'refactoring';
const courses = {
refactoring: ['Encapsulate Record', 'Encapsulate Collection'],
};
function readBasicCourseNames(filename) {
return courses[filename];
}
/**
* μμ μ½λ μ¬μ©
*/
const aPerson = new Person('kim');
const numAdvancedCourses = aPerson.courses.filter((c) => c.isAdvanced).length;
for (const name of readBasicCourseNames(filename)) {
aPerson.addCourse(new Course(name, false));
}
console.log(aPerson.name);
for (const course of aPerson.courses) {
console.log(course.name, course.isAdvanced);
}
console.log(numAdvancedCourses);