-
Notifications
You must be signed in to change notification settings - Fork 26.9k
/
es2015-getters-setters.html
69 lines (53 loc) · 1.56 KB
/
es2015-getters-setters.html
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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Object-oriented JavaScript inheritance</title>
</head>
<body>
</body>
<script>
// Basic Person class
class Person {
constructor(first, last, age, gender, interests) {
this.name = {
first,
last
};
this.age = age;
this.gender = gender;
this.interests = interests;
}
greeting() {
console.log(`Hi! I'm ${this.name.first}`);
};
farewell() {
console.log(`${this.name.first} has left the building. Bye for now!`);
};
}
// Extend Person with Teacher
class Teacher extends Person {
constructor(first, last, age, gender, interests, subject, grade) {
super(first, last, age, gender, interests);
// subject and grade are specific to Teacher
this._subject = subject;
this.grade = grade;
}
get subject() {
return this._subject;
}
set subject(newSubject) {
this._subject = newSubject;
}
}
// Instantiate Teacher
let snape = new Teacher('Severus', 'Snape', 58, 'male', ['Potions'], 'Dark arts', 5);
// Check the default value
console.log(snape.subject) // Returns "Dark arts"
// Change the value
snape.subject = "Balloon animals" // Sets subject to "Balloon animals"
// Check it again and see if it matches the new value
console.log(snape.subject) // Returns "Balloon animals"
</script>
</html>