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
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/15-function-expressions-arrows/article.md
+86-86Lines changed: 86 additions & 86 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -155,59 +155,59 @@ ask(
155
155
*/!*
156
156
```
157
157
158
-
Here, functions are declared right inside the`ask(...)` call. They have no name, and so are called *anonymous*. Such functions are not accessible outside of `ask` (because they are not assigned to variables), but that's just what we want here.
158
+
Di sini, fungsi dideklarasi tepat di dalam panggilan`ask(...)`. Mereka tak punya nama, jadinya disebut *anonymous*. Fungsi macam ini tak bisa diakses di luar `ask` (karena mereka tak diset ke variabel), tapi itulah yang kita mau di sini.
159
159
160
-
Such code appears in our scripts very naturally, it's in the spirit of JavaScript.
160
+
Kode macam ini muncul di script kita secara sangat alamiah, ia ada di dalam semangat JavaScript.
161
161
162
-
```smart header="A function is a value representing an \"action\""
163
-
Regular values like strings or numbers represent the *data*.
162
+
```smart header="Fungsi adalah nilai yang mewakili \"aksi\""
163
+
Nilai reguler seperti string atau angka mewakiliki *data*.
164
164
165
-
A function can be perceived as an *action*.
165
+
Fungsi bisa dianggap sebagai *aksi*.
166
166
167
-
We can pass it between variables and run when we want.
167
+
Kita bisa mengopernya antara variabel dan menjalankannya kapanpun kita mau.
168
168
```
169
169
170
170
171
-
## Function Expression vs Function Declaration
171
+
## Expresi Fungsi vs Deklarasi Fungsi
172
172
173
-
Let's formulate the key differences between Function Declarations and Expressions.
173
+
Mari kita rumuskan kunci perbedaan antara Deklarasi dan Expresi Fungsi.
174
174
175
-
First, the syntax: how to differentiate between them in the code.
175
+
Pertama, syntax: bagaimana membedakan antara mereka dalam kode.
176
176
177
-
-*Function Declaration:*a function, declared as a separate statement, in the main code flow.
177
+
-*Deklarasi Fungsi:*fungsi, yang dideklarasi sebagai pernyataan terpisah, dalam aliran kode utama.
178
178
179
179
```js
180
-
//Function Declaration
180
+
//Deklarasi Fungsi
181
181
functionsum(a, b) {
182
182
return a + b;
183
183
}
184
184
```
185
-
-*Function Expression:*a function, created inside an expression or inside another syntax construct. Here, the function is created at the right side of the "assignment expression" `=`:
185
+
-*Expresi Fungsi:*fungsi, yang dibuat dalam expresi atau dalam konstruksi syntax lain. Di sini, fungsi dibuat pada sisi kanan dari "expresi penetapan"`=`:
186
186
187
187
```js
188
-
//Function Expression
188
+
// Expresi Fungsi
189
189
let sum = function(a, b) {
190
190
return a + b;
191
191
};
192
192
```
193
193
194
-
The more subtle difference is *when* a function is created by the JavaScript engine.
194
+
Perbedaan yang lebih halus ialah *ketika* fungsi dibuat oleh JavaScript engine.
195
195
196
-
**A Function Expression is created when the execution reaches it and is usable only from that moment.**
196
+
**Expresi Fungsi dibuat ketika exekusi mencapainya dan hanya dapat dipakai saat itu saja.**
197
197
198
-
Once the execution flow passes to the right side of the assignment `let sum = function…` -- here we go, the function is created and can be used (assigned, called, etc. ) from now on.
198
+
Sekali aliran exekusi melewati sisi kanan penetapan `let sum = function…`--di sinilah, fungsi dibuat dan bisa digunakan (diset, dipanggil, dll. ) dari sekarang.
199
199
200
-
Function Declarations are different.
200
+
Deklarasi Fungsi berbeda.
201
201
202
-
**A Function Declaration can be called earlier than it is defined.**
202
+
**Deklarasi Fungsi bisa dipanggil lebih cepat dari ia didefinisi.**
203
203
204
-
For example, a global Function Declaration is visible in the whole script, no matter where it is.
204
+
Misalnya, Deklarasi Fungsi globalterlihat di seluruh script, tak peduli di mana ia berada.
205
205
206
-
That's due to internal algorithms. When JavaScript prepares to run the script, it first looks for global Function Declarations in it and creates the functions. We can think of it as an "initialization stage".
206
+
Itu karena algoritmainternal. Saat JavaScript siap menjalankan script, pertama ia melihat Deklarasi Fungsi globaldi dalam dan membuat fungsi. Kita bisa anggap itu sebagai "tahap inisialisasi".
207
207
208
-
And after all Function Declarations are processed, the code is executed. So it has access to these functions.
208
+
Dan setelah semua Deklarasi Fungsi diproses, kodenya diexekusi. Jadi ia punya akses ke fungsi-fungsi ini.
209
209
210
-
For example, this works:
210
+
Misalnya, ini berjalan:
211
211
212
212
```js run refresh untrusted
213
213
*!*
@@ -219,34 +219,34 @@ function sayHi(name) {
219
219
}
220
220
```
221
221
222
-
The Function Declaration `sayHi`is created when JavaScript is preparing to start the script and is visible everywhere in it.
222
+
Deklarasi Fungsi `sayHi`dibuat saat JavaScript siap memulai scriptnya dan terlihat di manapun di dalam.
223
223
224
-
...If it were a Function Expression, then it wouldn't work:
224
+
...Jika ia Expresi Fungsi, maka ia tak akan berjalan:
225
225
226
226
```js run refresh untrusted
227
227
*!*
228
-
sayHi("John"); //error!
228
+
sayHi("John"); // galat!
229
229
*/!*
230
230
231
-
letsayHi=function(name) { // (*) no magic any more
231
+
let sayHi = function(name) { // (*) tak ada magic lagi
232
232
alert( `Hello, ${name}` );
233
233
};
234
234
```
235
235
236
-
Function Expressions are created when the execution reaches them. That would happen only in the line `(*)`. Too late.
236
+
Expresi Fungsi dibuat saat exekusi mencapainya. Itu hanya akan terjadi pada baris `(*)`. Sudah telat banget.
237
237
238
-
Another special feature of Function Declarations is their block scope.
238
+
Fitur spesial lain dari Deklarasi Fungsi ialah scope blok mereka.
239
239
240
-
**In strict mode, when a Function Declaration is within a code block, it's visible everywhere inside that block. But not outside of it.**
240
+
**Di mode strict, ketika Deklarasi Fungsi berada di jangkauan blok kode, ia terlihat di manapun di dalam blok. Tapi tidak di luarnya.**
241
241
242
-
For instance, let's imagine that we need to declare a function `welcome()`depending on the `age`variable that we get during runtime. And then we plan to use it some time later.
242
+
Misalnya, bayangkan kita harus mendeklarasi fungsi `welcome()`tergantung variabel `age`yang kita dapat saat runtime. Lalu kita berencana akan menggunakannya kemudian.
243
243
244
-
If we use Function Declaration, it won't work as intended:
244
+
Jika kita memakai Deklarasi Fungsi, ia tak akan bekerja sesuai harapan:
245
245
246
246
```js run
247
247
let age = prompt("What is your age?", 18);
248
248
249
-
//conditionally declare a function
249
+
// secara kondisional mendeklarasi fungsi
250
250
if (age < 18) {
251
251
252
252
function welcome() {
@@ -261,30 +261,30 @@ if (age < 18) {
261
261
262
262
}
263
263
264
-
// ...use it later
264
+
// ...pakai ini kemudian
265
265
*!*
266
266
welcome(); // Error: welcome is not defined
267
267
*/!*
268
268
```
269
269
270
-
That's because a Function Declaration is only visible inside the code block in which it resides.
270
+
Itu karena Deklarasi Fungsi cuma terlihat di dalam blok kode yang ia tinggali.
271
271
272
-
Here's another example:
272
+
Ini contoh lainnya:
273
273
274
274
```js run
275
-
let age =16; //take 16 as an example
275
+
let age = 16; // ambil 16 sebagai contoh
276
276
277
277
if (age < 18) {
278
278
*!*
279
-
welcome(); // \ (runs)
279
+
welcome(); // \ (berjalan)
280
280
*/!*
281
281
// |
282
282
function welcome() { // |
283
-
alert("Hello!"); // | Function Declaration is available
284
-
} // | everywhere in the block where it's declared
283
+
alert("Hello!"); // | Deklarasi Fungsi tersedia
284
+
} // | di manapun dalam blok kode tempat dia dideklarasi
285
285
// |
286
286
*!*
287
-
welcome(); // / (runs)
287
+
welcome(); // / (berjalan)
288
288
*/!*
289
289
290
290
} else {
@@ -294,19 +294,19 @@ if (age < 18) {
294
294
}
295
295
}
296
296
297
-
//Here we're out of curly braces,
298
-
//so we can not see Function Declarations made inside of them.
297
+
// Di sini kita di luar kurung kurawal,
298
+
// jadi kita tak bisa melihat Deklarasi Fungsi yang dibuat di dalamnya.
299
299
300
300
*!*
301
301
welcome(); // Error: welcome is not defined
302
302
*/!*
303
303
```
304
304
305
-
What can we do to make`welcome`visible outside of`if`?
305
+
Apa yang bisa kita lakukan supaya`welcome`terlihat di luar`if`?
306
306
307
-
The correct approach would be to use a Function Expression and assign `welcome`to the variable that is declared outside of `if`and has the proper visibility.
307
+
Pendekatan yang benar ialah menggunakan Expresi Fungsi dan menetapkan `welcome`ke variabel yang dideklarasi di luar `if`dan punya visibilitas yang cukup.
308
308
309
-
This code works as intended:
309
+
Kode ini berjalan sesuai harapan:
310
310
311
311
```js run
312
312
let age = prompt("What is your age?", 18);
@@ -328,11 +328,11 @@ if (age < 18) {
328
328
}
329
329
330
330
*!*
331
-
welcome(); //ok now
331
+
welcome(); // sekarang ok
332
332
*/!*
333
333
```
334
334
335
-
Or we could simplify it even further using a question mark operator`?`:
335
+
Atau kita bisa menyederhanakannya lebih lanjut menggunakan operator tanda tanya`?`:
336
336
337
337
```js run
338
338
let age = prompt("What is your age?", 18);
@@ -342,47 +342,47 @@ let welcome = (age < 18) ?
342
342
function() { alert("Greetings!"); };
343
343
344
344
*!*
345
-
welcome(); //ok now
345
+
welcome(); // sekarang ok
346
346
*/!*
347
347
```
348
348
349
349
350
-
```smart header="When to choose Function Declaration versus Function Expression?"
351
-
As a rule of thumb, when we need to declare a function, the first to consider is Function Declaration syntax. It gives more freedom in how to organize our code, because we can call such functions before they are declared.
350
+
```smart header="Kapan harus memilih Deklarasi Fungsi versus Expresi Fungsi?"
351
+
Sebagai aturan praktis, saat kita harus mendeklarasi fungsi, hal pertama yang kita pertimbangkan ialah syntax Deklarasi Fungsi. Ia memberi kebebasan lebih dalam bagaimana mengorganisir kode kita, karena kita bisa memanggil fungsi macam ini sebelum mereka dideklarasi.
352
352
353
-
That's also better for readability, as it's easier to look up `function f(…) {…}` in the code than `let f = function(…) {…}`. Function Declarations are more "eye-catching".
353
+
Itu juga untuk keterbacaan yang lebih baik, karena lebih mudah melihat`functionf(…) {…}`dalam kode ketimbang `letf=function(…) {…}`. Deklarasi Fungsi lebih "eye-catching".
354
354
355
-
...But if a Function Declaration does not suit us for some reason, or we need a conditional declaration (we've just seen an example), then Function Expression should be used.
355
+
...Tapi jika Deklarasi Fungsi tak cocok untuk beberapa alasan, atau kita butuh deklarasi kondisional (kita sudah lihat contohnya), maka Expresi Fungsi sebaiknya digunakan.
356
356
```
357
357
358
358
359
-
## Arrow functions[#arrow-functions]
359
+
## Fungsi panah [#arrow-functions]
360
360
361
-
There's one more very simple and concise syntax for creating functions, that's often better than Function Expressions. It's called "arrow functions", because it looks like this:
361
+
Ada lagi syntax yang lebih simpel dan ringkas untuk membuat fungsi, ia lebih baik dari Expresi Fungsi. Ia disebut "fungsi panah", karena rupanya seperti ini:
362
362
363
363
364
364
```js
365
365
let func = (arg1, arg2, ...argN) => expression
366
366
```
367
367
368
-
...This creates a function `func`that has arguments`arg1..argN`, evaluates the `expression`on the right side with their use and returns its result.
368
+
...Ini membuat fungsi `func`yang punya argumen`arg1..argN`, mengevaluasi `expression`di sisi kanan dengan penggunaan mereka dan mengembalikan hasilnya.
369
369
370
-
In other words, it's roughly the same as:
370
+
Dengan kata lain, kasarnya ia sama saja dengan:
371
371
372
372
```js
373
373
let func = function(arg1, arg2, ...argN) {
374
374
return expression;
375
375
};
376
376
```
377
377
378
-
...But much more concise.
378
+
...Tapi lebih ringkas.
379
379
380
-
Let's see an example:
380
+
Ayo kita lihat contoh:
381
381
382
382
```js run
383
383
let sum = (a, b) => a + b;
384
384
385
-
/*The arrow function is a shorter form of:
385
+
/* Fungsi panah ialah bentuk pendek dari:
386
386
387
387
let sum = function(a, b) {
388
388
return a + b;
@@ -393,10 +393,10 @@ alert( sum(1, 2) ); // 3
393
393
394
394
```
395
395
396
-
If we have only one argument, then parentheses around parameters can be omitted, making that even shorter:
396
+
Jika kita cuma punya satu argumen, maka tanda kurung sekitar parameter bisa dibuang, membuatnya lebih pendek lagi:
397
397
398
398
```js run
399
-
//same as
399
+
// sama dengan
400
400
// let double = function(n) { return n * 2 }
401
401
*!*
402
402
let double = n => n * 2;
@@ -405,17 +405,17 @@ let double = n => n * 2;
405
405
alert( double(3) ); // 6
406
406
```
407
407
408
-
If there are no arguments, parentheses should be empty (but they should be present):
408
+
Jika tak ada argumen, tanda kurung sebaiknya kosong (tapi mereka harus ada):
409
409
410
410
```js run
411
411
let sayHi = () => alert("Hello!");
412
412
413
413
sayHi();
414
414
```
415
415
416
-
Arrow functions can be used in the same way as Function Expressions.
416
+
Panah fungsi bisa digunakan dengan cara yang sama dengan Expresi Fungsi.
417
417
418
-
For instance, here's the rewritten example with`welcome()`:
418
+
Misalnya, ini contoh yang ditulis ulang dengan`welcome()`:
419
419
420
420
```js run
421
421
let age = prompt("What is your age?", 18);
@@ -427,48 +427,48 @@ let welcome = (age < 18) ?
427
427
welcome(); // ok now
428
428
```
429
429
430
-
Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure.
430
+
Fungsi panah mungkin terlihat asing dan tak mudah terbaca pertama kali, tapi itu cepat berubah seiring mata memandangi strukturnya.
431
431
432
-
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
432
+
Mereka sangat nyaman untuk aksi sebaris simpel, saat kita malas banget untuk menulis banyak kata.
433
433
434
-
```smart header="Multiline arrow functions"
434
+
```smart header="Fungsi panah baris-ganda"
435
435
436
-
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
436
+
Contoh di atas mengambil argumen dari kiri `=>`dan mengealuasi expresi sisi kanan expression dengan mereka.
437
437
438
-
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
438
+
kadang kita butuh sesuatu yang lebih komplex, seperti pernyataan atau expresi ganda. Itu juga memungkinkan, tapi sebaiknya kita melampirkan mereka dalam kurung kurawal. Lalu gunakan `return`normal di dalamnya.
439
439
440
-
Like this:
440
+
Seperti ini:
441
441
442
442
```js run
443
-
let sum = (a, b) => { // the curly brace opens a multiline function
443
+
letsum= (a, b) => { //kurung kurawal membuka fungsi baris-ganda
444
444
let result = a + b;
445
445
*!*
446
-
return result; // if we use curly braces, use return to get results
446
+
return result; //jika kita menggunakan kurung kurawal, gunakan return untuk mendapatkan hasil
447
447
*/!*
448
448
};
449
449
450
450
alert( sum(1, 2) ); // 3
451
451
```
452
452
453
-
```smart header="More to come"
454
-
Here we praised arrow functions for brevity. But that's not all! Arrow functions have other interesting features. We'll return to them later in the chapter <info:arrow-functions>.
453
+
```smart header="Ada lagi"
454
+
Di sini kita memuji fungsi panah karena keringkasan. Tapi belum selesai! Fungsi panah punya fitur menarik lain. Kita akan kembali ke mereka nanti di bab <info:arrow-functions>.
455
455
456
-
For now, we can already use arrow functions for one-line actions and callbacks.
456
+
Untuk sekarang, kita sudah bisa menggunakan panah fungsi untuk aksi sebaris dan callback.
457
457
```
458
458
459
-
## Summary
459
+
## Kesimpulan
460
460
461
-
-Functions are values. They can be assigned, copied or declared in any place of the code.
462
-
-If the function is declared as a separate statement in the main code flow, that's called a "Function Declaration".
463
-
-If the function is created as a part of an expression, it's called a "Function Expression".
464
-
-Function Declarations are processed before the code block is executed. They are visible everywhere in the block.
465
-
-Function Expressions are created when the execution flow reaches them.
461
+
-Fungsi adalah nilai. Mereka bisa diset, dikopi atau dideklarasi di kode manapun.
462
+
-Jika fungsi dideklarasi sebagai pernyataan terpisah di aliran kode utama, ia disebut "Deklarasi Fungsi".
463
+
-Jika fungsi dibuat sebagai bagian expresi, ia disebut "Expresi Fungsi".
464
+
-Deklarasi Fungsi diproses sebelum blok kode diexekusi. Mereka terlihat di manapun dalam blok.
465
+
-Expresi Fungsi dibuat saat aliran exekusi mencapai mereka.
466
466
467
-
In most cases when we need to declare a function, a Function Declaration is preferable, because it is visible prior to the declaration itself. That gives us more flexibility in code organization, and is usually more readable.
467
+
Di banyak kasus saat kita harus mendeklarasi fungsi, Deklarasi Fungsi disenangi, karena ia terlihat sebelum deklarasi itu sendiri. Itu memberi kita flexibilitas lebih dalam organisasi kode, dan biasa lebih mudah terbaca.
468
468
469
-
So we should use a Function Expression only when a Function Declaration is not fit for the task. We've seen a couple of examples of that in this chapter, and will see more in the future.
469
+
Jadi sebaiknya kita gunakan Expresi Fungsi hanya saat Deklarasi Fungsi tak cocok digunakan. Kita sudah melihat beberapa contoh itu di bab ini, dan kita akan melihat lebih lagi nanti.
470
470
471
-
Arrow functions are handy for one-liners. They come in two flavors:
471
+
Panah fungsi lebih praktis untuk satu baris. Mereka ada dalam dua rasa:
472
472
473
-
1.Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
474
-
2.With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return`to return something.
473
+
1.Tanpa kurung kurawal: `(...args) => expression` -- sisi kanan ialah expresi: fungsi mengevaluasinya dan mengembalikan hasil.
474
+
2.Dengan kurung kurawal: `(...args) => { body }` -- bracket memperbolehkan kita menulis pernyataan ganda di dalam fungsi, tapi kita butuh `return`explisit untuk mengembalikan sesuatu.
0 commit comments