-
Notifications
You must be signed in to change notification settings - Fork 0
/
getter-and-setter.html
83 lines (74 loc) · 2.54 KB
/
getter-and-setter.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!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>Getter dan Setter</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
padding: 2rem;
}
p {
padding: .5rem 0;
}
div {
width: 100%;
height: 100vh;
background-color: rgb(0, 195, 255);
font-weight: 800;
font-size: 2rem;
font-family: sans-serif;
text-transform: uppercase;
display: flex;
justify-content: center;
align-items: center;
color: #0e0e0e;
}
</style>
</head>
<body>
<script>
/*
## Getter dan Setter
● Getter dan Setter adalah kemampuan membuat function yang berbeda untuk mengambil data (Getter) dan mengubah data (Setter) pada sebuah property di Object
● Dengan menggunakan Getter dan Setter, kita bisa melakukan hal apapun dalam function sebelum sebuah property di akses atau diubah, misal menambah validasi dan lain-lain
*/
// =====Kode : Getter=====
document.writeln(`<p>=====Getter=====</p>`);
const person = {
firstName: "Fauzan",
lastName: "Ahmad",
get fullName() {
// mengconstruct datanya secara dynamic
return `${this.firstName} ${this.lastName}`;
},
set fullName(val) {
const array = val.split(" ");
this.firstName = array[0];
this.lastName = array[1];
}
}
// otomatis memanggil get fullName()
document.writeln(`<p>${person.fullName}</p>`);
// =====Kode : Setter=====
document.writeln(`<p>=====Setter=====</p>`);
person.fullName = "Antonio DeLuza";
document.writeln(`<p>${person.firstName}</p>`);
document.writeln(`<p>${person.lastName}</p>`);
document.writeln(`<p>=====Setter=====</p>`);
person.fullName = "Lambe Turah";
document.writeln(`<p>${person.firstName}</p>`);
document.writeln(`<p>${person.lastName}</p>`);
document.writeln(`<p>=====Setter=====</p>`);
person.fullName = "Turah Partai";
document.writeln(`<p>${person.firstName}</p>`);
document.writeln(`<p>${person.lastName}</p>`);
</script>
</body>
</html>