-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20_classesAdvanced.js
97 lines (71 loc) · 2.36 KB
/
20_classesAdvanced.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
/*-----------------------------------------------------------------*/
/* Getters and setters in Classes
-------------------------------------------------------------------*/
// NOTE
// The underscore in front of the properties is another example of a convention.
// It also prevents a stack overflow when calling our methods. Also, note that we
// are calling “jeff.name” not “jeff._name”. So the output is being returned from our getter.
class Person {
constructor(fullname, birthYear) {
this.fullname = fullname
this.birthYear = birthYear
}
set fullname(name) {
if (name.includes(' ')) {
this._fullname = name
} else {
console.log(`${name} is not a full name!`);
}
}
get fullname() {
return this._fullname
}
}
const dima = new Person("Dmytro Anikin", 2000);
dima.fullname = "Dima Anikin"
console.log(dima.fullname);
/*-----------------------------------------------------------------*/
/* Protected properties && methods
-------------------------------------------------------------------*/
class User {
constructor(username, pin) {
this.username = username
// Protected property (_ convention). Shouldn't be used outside of class.
this._pin = pin
}
// Public method to access the protected property
getPin() {
return this._pin
}
// Protected method (_ convention). Shouldn't be used outside of class.
_internalMethod() {
return 'Dummy text'
}
}
/*-----------------------------------------------------------------*/
/* Public/Private fields && methods -- VERY RECENT FEATURE IN JS (2022)
-------------------------------------------------------------------*/
class Animal {
// Public fields (present on all the instances)
environments = [];
// Private fields (cannot be read from outside the class)
#population = 10;
#exists;
constructor(type, legs, exists) {
this.type = type
this.legs = legs
this.#exists = exists
}
// The only way to access private fields (they will never show up otherwise)
getPopulation(){
return this.#population
}
getExists() {
return this.#exists
}
#privateMethod(){
console.log("I am a private method!");
}
}
const dog = new Animal("dog", 4, true)
console.log(dog.getExists());