You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you're starting to work with events -- please note some subtleties.
141
+
Jika kamu mulai bekerja dengan menggunakan peristiwa -- harap perhatikan beberapa detail.
142
142
143
-
We can set an existing function as a handler:
143
+
Kita bisa mengatur sebuah fungsi yang telah ada sebagai pengendali:
144
144
145
145
```js
146
-
functionsayThanks() {
147
-
alert('Thanks!');
146
+
functionucapkanTerimaKasih() {
147
+
alert('Terima Kasih!');
148
148
}
149
149
150
-
elem.onclick=sayThanks;
150
+
elem.onclick=ucapkanTerimaKasih;
151
151
```
152
-
153
-
But be careful: the function should be assigned as `sayThanks`, not `sayThanks()`.
152
+
Tetapi berhati-hatilah: fungsi harus di atur sebagai `ucapkanTerimaKasih`, bukan `ucapkanTerimaKasih()`.
154
153
155
154
```js
156
-
//right
157
-
button.onclick=sayThanks;
155
+
//benar
156
+
button.onclick=ucapkanTerimaKasih;
158
157
159
-
//wrong
160
-
button.onclick=sayThanks();
158
+
//salah
159
+
button.onclick=ucapkanTerimaKasih();
161
160
```
161
+
Jika kita tambahkan tanda kurung, maka `ucapkanTerimaKasih()` menjadi proses pemanggilan fungsi. Jadi baris terakhir akan mengambil *hasil* dari pengeksekusian fungsi, yang merupakan `tidak terdefinisi` (_`undefined`_ — karena fungsi tidak mengembalikan apapun), dan mengatur nilai itu ke peristiwa `onclick`. Maka peristiwa tersebut tidak akan menjalankan apapun.
162
162
163
-
If we add parentheses, then `sayThanks()` becomes is a function call. So the last line actually takes the *result* of the function execution, that is `undefined` (as the function returns nothing), and assigns it to `onclick`. That doesn't work.
164
-
165
-
...On the other hand, in the markup we do need the parentheses:
163
+
...Namun, jika kita menambahkan secara langsung ke HTML, maka kita harus menambahkan tanda kurung:
The difference is easy to explain. When the browser reads the attribute, it creates a handler function with body from the attribute content.
169
+
Perbedaannya mudah untuk di jelaskan. Pada saat peramban membaca atribute, peramban akan membuat fungsi pengendali yang didalamnya terdapat konten dari atribut tersebut.
172
170
173
-
So the markup generates this property:
171
+
Jadi HTML akan menghasilkan properti ini:
174
172
```js
175
173
button.onclick=function() {
176
174
*!*
177
-
sayThanks(); // <-- the attribute content goes here
175
+
ucapkanTerimaKasih(); // <-- konten dari atribut akan ditambahkan kesini
178
176
*/!*
179
177
};
180
178
```
181
179
182
-
**Don't use`setAttribute`for handlers.**
180
+
**Jangan gunakna`setAttribute`untuk membuat pengendali.**
183
181
184
-
Such a call won't work:
182
+
Penggunaan tersebut tidak akan berjalan:
185
183
186
184
```js run no-beautify
187
-
//a click on <body> will generate errors,
188
-
//because attributes are always strings, function becomes a string
185
+
//sebuah klik pada <body> akan menghasilakn eror
186
+
//karena atribute akan selalu menjadi teks (string), dimana fungsi akan menjadi teks (string)
The fundamental problem of the aforementioned ways to assign handlers -- we can't assign multiple handlers to one event.
196
+
Salah satu masalah mendasar pada cara mengatur pengedali sebelumnya -- kita tidak bisa mengatur beberapa pengendali pada sebuah peristiwa.
199
197
200
-
Let's say, one part of our code wants to highlight a button on click, and another one wants to show a message on the same click.
198
+
Mari kata, sebuah bagian pada koded kita ingin menyoroti sebuah tombol pada saat diklik, dan satu lagi ingin menunjukan seubah pesan pada proses pengklikan tersebut.
201
199
202
-
We'd like to assign two event handlers for that. But a new DOM property will overwrite the existing one:
200
+
Kita ingin mengatur dua pengendali peristiwa untuk hal tersebut. Tapi properti DOM yang baru akan menimpa properti DOM yang telah ada.
203
201
204
202
```js no-beautify
205
203
input.onclick=function() { alert(1); }
206
204
// ...
207
-
input.onclick=function() { alert(2); } //replaces the previous handler
205
+
input.onclick=function() { alert(2); } //menganti pengedali yang lama
208
206
```
207
+
Pengembang dari standar situs web paham sejak lama, dan menyarankan cara alternatif untuk mengelola pengendali menggunakan metode khusus `addEventListener` dan `removeEventListener`. Kedua hal tersebut tidak memiliki permasalahan seperti itu.
209
208
210
-
Developers of web standards understood that long ago and suggested an alternative way of managing handlers using special methods `addEventListener` and `removeEventListener`. They are free of such a problem.
211
-
212
-
The syntax to add a handler:
209
+
Sintaks (_syntax_) untuk menambahkan sebuah pengendali:
- `once`: if`true`, then the listener is automatically removed after it triggers.
227
-
- `capture`: the phase where to handle the event, to be covered later in the chapter<info:bubbling-and-capturing>. For historical reasons, `options`can also be`false/true`, that's the same as`{capture: false/true}`.
228
-
- `passive`: if`true`, then the handler will not call `preventDefault()`, we'll explain that later in<info:default-browser-action>.
221
+
`pilihan`/`options`
222
+
: sebuah objek pilihan tambahan dengan properti:
223
+
- `once`: jika`true`, maka pendengar akan secara otomatis dihapus setelah terpicu.
224
+
- `capture`: fase dimana untuk menangani peristiwa, akan di bahas lebih lanjut pada bab<info:bubbling-and-capturing>. untuk alasan sejarah, `options`bisa juga diatur`false/true`, sama halnya dengan`{capture: false/true}`.
225
+
- `passive`: jika`true`, maka pengendali tidak akan memanggil `preventDefault()`, kita akan membahas lebih lanjut pada bab<info:default-browser-action>.
229
226
230
-
To remove the handler, use`removeEventListener`:
227
+
Untuk menghapus pengendali, gunakan`removeEventListener`:
The handler won't be removed, because `removeEventListener` gets another function -- with the same code, but that doesn't matter, as it's a different function object.
244
+
Pengedali tidak akan dihapus, karena `removeEventListener` mendapat sebuah fungsi lain -- dengan kode yang sama, tetapi hal tersebut tidak penting, karena itu merupakan objek fungsi yang berbeda.
0 commit comments