Skip to content

Commit e059620

Browse files
committed
Translate Event: Browser-Event/Article.md - 50%
1 parent 83a7bf5 commit e059620

File tree

1 file changed

+52
-55
lines changed
  • 2-ui/2-events/01-introduction-browser-events

1 file changed

+52
-55
lines changed

2-ui/2-events/01-introduction-browser-events/article.md

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -126,131 +126,128 @@ Pada contoh dibawah menambah sebuah pengendali menggunakan Javascript akan menim
126126

127127
Untuk menghapus sebuah pengendali -- atur `elem.onclick = null`
128128

129-
## Accessing the element: this
129+
## Mengakses elemen: this
130130

131-
The value of `this` inside a handler is the element. The one which has the handler on it.
131+
nilai dari `this` didalam pengendali adalah elemen tersebut. Elemen yang dimana pengendali itu berada.
132132

133-
In the code below `button` shows its contents using `this.innerHTML`:
133+
Pada kode dibawah `button` menampilkan kontennya dengan menggunakan `this.innerHTML`:
134134

135135
```html height=50 autorun
136-
<button onclick="alert(this.innerHTML)">Click me</button>
136+
<button onclick="alert(this.innerHTML)">Klik saya</button>
137137
```
138138

139-
## Possible mistakes
139+
## Kemungkinan kesalahan
140140

141-
If you're starting to work with events -- please note some subtleties.
141+
Jika kamu mulai bekerja dengan menggunakan peristiwa -- harap perhatikan beberapa detail.
142142

143-
We can set an existing function as a handler:
143+
Kita bisa mengatur sebuah fungsi yang telah ada sebagai pengendali:
144144

145145
```js
146-
function sayThanks() {
147-
alert('Thanks!');
146+
function ucapkanTerimaKasih() {
147+
alert('Terima Kasih!');
148148
}
149149

150-
elem.onclick = sayThanks;
150+
elem.onclick = ucapkanTerimaKasih;
151151
```
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()`.
154153

155154
```js
156-
// right
157-
button.onclick = sayThanks;
155+
// benar
156+
button.onclick = ucapkanTerimaKasih;
158157

159-
// wrong
160-
button.onclick = sayThanks();
158+
// salah
159+
button.onclick = ucapkanTerimaKasih();
161160
```
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.
162162

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:
166164

167165
```html
168-
<input type="button" id="button" onclick="sayThanks()">
166+
<input type="button" id="button" onclick="ucapkanTerimaKasih()">
169167
```
170168

171-
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.
172170

173-
So the markup generates this property:
171+
Jadi HTML akan menghasilkan properti ini:
174172
```js
175173
button.onclick = function() {
176174
*!*
177-
sayThanks(); // <-- the attribute content goes here
175+
ucapkanTerimaKasih(); // <-- konten dari atribut akan ditambahkan kesini
178176
*/!*
179177
};
180178
```
181179

182-
**Don't use `setAttribute` for handlers.**
180+
**Jangan gunakna `setAttribute` untuk membuat pengendali.**
183181

184-
Such a call won't work:
182+
Penggunaan tersebut tidak akan berjalan:
185183

186184
```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)
189187
document.body.setAttribute('onclick', function() { alert(1) });
190188
```
191189

192-
**DOM-property case matters.**
190+
**Properti DOM mementingkan kesamaan huruf.**
193191

194-
Assign a handler to `elem.onclick`, not `elem.ONCLICK`, because DOM properties are case-sensitive.
192+
Atur sebuah pengendali ke `elem.onclick`, bukan `elem.ONCLICK`, karena properti DOM mementingkan kesamaan huruf (_case-sensitive_).
195193

196-
## addEventListener
194+
## tambahkanPendengarPeristiwa (_addEventListener_)
197195

198-
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.
199197

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.
201199

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.
203201

204202
```js no-beautify
205203
input.onclick = function() { alert(1); }
206204
// ...
207-
input.onclick = function() { alert(2); } // replaces the previous handler
205+
input.onclick = function() { alert(2); } // menganti pengedali yang lama
208206
```
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.
209208

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:
213210

214211
```js
215212
element.addEventListener(event, handler, [options]);
216213
```
217214

218-
`event`
219-
: Event name, e.g. `"click"`.
215+
`peristiwa`/`event`
216+
: nama Peristiwa, contoh `"click"`.
220217

221-
`handler`
222-
: The handler function.
218+
`pengendali`/`handler`
219+
: pengendali fungsi.
223220

224-
`options`
225-
: An additional optional object with properties:
226-
- `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>.
229226

230-
To remove the handler, use `removeEventListener`:
227+
Untuk menghapus pengendali, gunakan `removeEventListener`:
231228

232229
```js
233230
element.removeEventListener(event, handler, [options]);
234231
```
235232

236-
````warn header="Removal requires the same function"
237-
To remove a handler we should pass exactly the same function as was assigned.
233+
````warn header="Penghapusan membutuhkan fungsi yang sama"
234+
Untuk menghapus sebuah pengendali kita melewatkan fungsi yang sama dengan yang kita atur.
238235
239-
This doesn't work:
236+
Ini tidak akan berfungsi:
240237
241238
```js no-beautify
242-
elem.addEventListener( "click" , () => alert('Thanks!'));
239+
elem.addEventListener( "click" , () => alert('Terima Kasih!'));
243240
// ....
244-
elem.removeEventListener( "click", () => alert('Thanks!'));
241+
elem.removeEventListener( "click", () => alert('Terima Kasih!'));
245242
```
246243
247-
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.
248245
249-
Here's the right way:
246+
Inilah cara yang benar:
250247
251248
```js
252249
function handler() {
253-
alert( 'Thanks!' );
250+
alert( 'Terima Kasih!' );
254251
}
255252
256253
input.addEventListener("click", handler);

0 commit comments

Comments
 (0)