-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_es6.js
93 lines (84 loc) · 2.12 KB
/
3_es6.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
/**
* ES6 Way
* Essentially, this is just syntax sugar, that under the hood does the same
* thing as previous ones.
*/
/**
* Person class.
*/
class Person {
/**
* Static method to return the top boy name and girl name.
* @returns {Array} top boy name and girl name as an array
*/
static topNames() {
return ['Mark', 'Lily'];
}
/**
* Constructor with parameter.
* @param {string} firstName first name of the person
* @param {string} lastName last name of the person
* @param {string} dateOfBirth date of birth of the person as a string
*/
constructor(firstName, lastName, dateOfBirth) {
const dob = new Date(dateOfBirth);
Object.assign(this, { firstName, lastName, dob });
}
/**
* Returns the full name of this person.
* @returns {string} full name
*/
getFullName() {
return `${this.firstName} ${this.lastName}`;
}
/**
* Returns the birth year of this person.
* @returns {number} birth year of this person
*/
getBirthYear() {
return this.dob.getFullYear();
}
/**
* Does a self-introduction.
* @returns {string} self-introduction of this person
*/
selfIntro() {
return `Hi, my name is ${this.getFullName()}`;
}
}
// Inheritance
/**
* Student class.
*/
class Student extends Person {
/**
* Constructor with parameter.
* @param {string} firstName first name of the student
* @param {string} lastName last name of the student
* @param {string} dateOfBirth date of birth of the student
* @param {number} studentId student ID
*/
constructor(firstName, lastName, dateOfBirth, studentId) {
super(firstName, lastName, dateOfBirth);
this.studentId = studentId;
}
/**
* Accessor of studentId.
* Note how this is defined as a getter method.
* @returns {number} studentId
*/
get studentID() {
return this.studentId;
}
/**
* Mutator of studentId.
* Note how this is defined as a setter method.
* @param studentId student ID to set
*/
set studentID(studentId) {
this.studentId = studentId;
}
selfIntro() {
return `${super.selfIntro()}, and my student ID is ${this.studentId}`;
}
}