-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClass-Field.html
More file actions
96 lines (76 loc) · 2.32 KB
/
Class-Field.html
File metadata and controls
96 lines (76 loc) · 2.32 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Class Field</title>
</head>
<body>
<script>
// Public Class Field
class thisPublicField {
// Create field class
firstName;
lastName;
balance = 0;
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
const publicClass = new thisPublicField("Eko", "Kurniawan");
console.log(publicClass);
// Private Class Field, can only be accessed in the class only
class thisPrivateField {
//Create field class
#counter = 0; //To create a private class must add #
// method
increment() {
this.#counter++;
}
decrement() {
this.#counter--;
}
get() {
return this.#counter;
}
}
const counter = new thisPrivateField();
counter.increment();
counter.increment();
counter.increment();
counter.increment();
counter.increment();
// counter.counter = 100; ==> error
console.log(counter.get());
// Private Method
class thisPrivateMethod {
say(name) {
if (name) {
this.#sayWithName(name);
} else {
this.#sayWithoutName();
}
}
#sayWithoutName() {
console.log("Hello");
}
#sayWithName(name) {
console.log(`Hello ${name}`);
}
}
const adrian = new thisPrivateMethod();
adrian.say("Adrian");
//Static Class Field, jadi field nya milik class, bukan lagi miliki object
class ThisStaticField {
static nameTitle = "Learn OOP";
static version = 1.0;
static author = "Adrian Miftahul Haq";
}
console.info(ThisStaticField.nameTitle);
console.info(ThisStaticField.version);
console.info(ThisStaticField.author);
</script>
</body>
</html>