-
Notifications
You must be signed in to change notification settings - Fork 0
/
refactoring-step-1.js
51 lines (47 loc) · 1.02 KB
/
refactoring-step-1.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
class Person {
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 {
constructor(name, isAdvanced) {
this._name = name
this._isAdvanced = isAdvanced
}
get name() {
return this._name
}
get isAdvanced() {
return this._isAdvanced
}
}
// client
numAdvancedCourses = aPerson.courses.filter((c) => c.isAdvanced).length
const basicCourseNames = readBasicCourseNames(filename)
aPerson.courses = basicCourseNames.map((name) => new Course(name, false))
for (const name of readBasicCourseNames(filename)) {
aPerson.addCourse(new Course(name, false))
}