-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccessors.js
More file actions
55 lines (42 loc) · 937 Bytes
/
Accessors.js
File metadata and controls
55 lines (42 loc) · 937 Bytes
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
//: 21.1 => Accessor functions for properties are not required
//: 21.2 => /* Do not use JavaScript getters/setters as they cause unexpected side effects and harder to test, maintainm and reason about. Instead, if you do make accessor functions, use getVal() and setVal("hello") * /
// bad
class Dragon {
get age() {
//...
}
set age(value) {
//...
}
}
// good
class Dragon {
getAge() {
// ...
}
setAge(value) {
// ...
}
}
//: 21.3 => If the property/method is a boolean, use isVal() or hasVal().
// bad
if (!dragon.age()) {
return false;
}
// good
if (!dragon.hasAge()) {
return false;
}
//: 21.4 => It's okay to create get() and set() function, but be consistent.
class Jedi {
constructor(options = {}) {
const lightsaber = options.lightsaber || "blue";
this.set("lightsaber", lightsaber);
}
set(key, val) {
this[key] = val;
}
get(key) {
return this[key];
}
}