Skip to content

Commit ec939b1

Browse files
committed
feat(i18n): 1.02.15.article
1 parent dae1af3 commit ec939b1

File tree

1 file changed

+86
-86
lines changed
  • 1-js/02-first-steps/15-function-expressions-arrows

1 file changed

+86
-86
lines changed

1-js/02-first-steps/15-function-expressions-arrows/article.md

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -155,59 +155,59 @@ ask(
155155
*/!*
156156
```
157157

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

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

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*.
164164
165-
A function can be perceived as an *action*.
165+
Fungsi bisa dianggap sebagai *aksi*.
166166
167-
We can pass it between variables and run when we want.
167+
Kita bisa mengopernya antara variabel dan menjalankannya kapanpun kita mau.
168168
```
169169

170170

171-
## Function Expression vs Function Declaration
171+
## Expresi Fungsi vs Deklarasi Fungsi
172172

173-
Let's formulate the key differences between Function Declarations and Expressions.
173+
Mari kita rumuskan kunci perbedaan antara Deklarasi dan Expresi Fungsi.
174174

175-
First, the syntax: how to differentiate between them in the code.
175+
Pertama, syntax: bagaimana membedakan antara mereka dalam kode.
176176

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

179179
```js
180-
// Function Declaration
180+
// Deklarasi Fungsi
181181
function sum(a, b) {
182182
return a + b;
183183
}
184184
```
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" `=`:
186186

187187
```js
188-
// Function Expression
188+
// Expresi Fungsi
189189
let sum = function(a, b) {
190190
return a + b;
191191
};
192192
```
193193

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

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.**
197197

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

200-
Function Declarations are different.
200+
Deklarasi Fungsi berbeda.
201201

202-
**A Function Declaration can be called earlier than it is defined.**
202+
**Deklarasi Fungsi bisa dipanggil lebih cepat dari ia didefinisi.**
203203

204-
For example, a global Function Declaration is visible in the whole script, no matter where it is.
204+
Misalnya, Deklarasi Fungsi global terlihat di seluruh script, tak peduli di mana ia berada.
205205

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 algoritma internal. Saat JavaScript siap menjalankan script, pertama ia melihat Deklarasi Fungsi global di dalam dan membuat fungsi. Kita bisa anggap itu sebagai "tahap inisialisasi".
207207

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

210-
For example, this works:
210+
Misalnya, ini berjalan:
211211

212212
```js run refresh untrusted
213213
*!*
@@ -219,34 +219,34 @@ function sayHi(name) {
219219
}
220220
```
221221

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

224-
...If it were a Function Expression, then it wouldn't work:
224+
...Jika ia Expresi Fungsi, maka ia tak akan berjalan:
225225

226226
```js run refresh untrusted
227227
*!*
228-
sayHi("John"); // error!
228+
sayHi("John"); // galat!
229229
*/!*
230230
231-
let sayHi = function(name) { // (*) no magic any more
231+
let sayHi = function(name) { // (*) tak ada magic lagi
232232
alert( `Hello, ${name}` );
233233
};
234234
```
235235

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

238-
Another special feature of Function Declarations is their block scope.
238+
Fitur spesial lain dari Deklarasi Fungsi ialah scope blok mereka.
239239

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.**
241241

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

244-
If we use Function Declaration, it won't work as intended:
244+
Jika kita memakai Deklarasi Fungsi, ia tak akan bekerja sesuai harapan:
245245

246246
```js run
247247
let age = prompt("What is your age?", 18);
248248
249-
// conditionally declare a function
249+
// secara kondisional mendeklarasi fungsi
250250
if (age < 18) {
251251
252252
function welcome() {
@@ -261,30 +261,30 @@ if (age < 18) {
261261
262262
}
263263
264-
// ...use it later
264+
// ...pakai ini kemudian
265265
*!*
266266
welcome(); // Error: welcome is not defined
267267
*/!*
268268
```
269269

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

272-
Here's another example:
272+
Ini contoh lainnya:
273273

274274
```js run
275-
let age = 16; // take 16 as an example
275+
let age = 16; // ambil 16 sebagai contoh
276276
277277
if (age < 18) {
278278
*!*
279-
welcome(); // \ (runs)
279+
welcome(); // \ (berjalan)
280280
*/!*
281281
// |
282282
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
285285
// |
286286
*!*
287-
welcome(); // / (runs)
287+
welcome(); // / (berjalan)
288288
*/!*
289289
290290
} else {
@@ -294,19 +294,19 @@ if (age < 18) {
294294
}
295295
}
296296
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.
299299
300300
*!*
301301
welcome(); // Error: welcome is not defined
302302
*/!*
303303
```
304304

305-
What can we do to make `welcome` visible outside of `if`?
305+
Apa yang bisa kita lakukan supaya `welcome` terlihat di luar `if`?
306306

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

309-
This code works as intended:
309+
Kode ini berjalan sesuai harapan:
310310

311311
```js run
312312
let age = prompt("What is your age?", 18);
@@ -328,11 +328,11 @@ if (age < 18) {
328328
}
329329
330330
*!*
331-
welcome(); // ok now
331+
welcome(); // sekarang ok
332332
*/!*
333333
```
334334

335-
Or we could simplify it even further using a question mark operator `?`:
335+
Atau kita bisa menyederhanakannya lebih lanjut menggunakan operator tanda tanya `?`:
336336

337337
```js run
338338
let age = prompt("What is your age?", 18);
@@ -342,47 +342,47 @@ let welcome = (age < 18) ?
342342
function() { alert("Greetings!"); };
343343
344344
*!*
345-
welcome(); // ok now
345+
welcome(); // sekarang ok
346346
*/!*
347347
```
348348

349349

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.
352352
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 `function f(…) {…}` dalam kode ketimbang `let f = function(…) {…}`. Deklarasi Fungsi lebih "eye-catching".
354354
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.
356356
```
357357

358358

359-
## Arrow functions [#arrow-functions]
359+
## Fungsi panah [#arrow-functions]
360360

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

363363

364364
```js
365365
let func = (arg1, arg2, ...argN) => expression
366366
```
367367

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

370-
In other words, it's roughly the same as:
370+
Dengan kata lain, kasarnya ia sama saja dengan:
371371

372372
```js
373373
let func = function(arg1, arg2, ...argN) {
374374
return expression;
375375
};
376376
```
377377

378-
...But much more concise.
378+
...Tapi lebih ringkas.
379379

380-
Let's see an example:
380+
Ayo kita lihat contoh:
381381

382382
```js run
383383
let sum = (a, b) => a + b;
384384
385-
/* The arrow function is a shorter form of:
385+
/* Fungsi panah ialah bentuk pendek dari:
386386
387387
let sum = function(a, b) {
388388
return a + b;
@@ -393,10 +393,10 @@ alert( sum(1, 2) ); // 3
393393
394394
```
395395

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

398398
```js run
399-
// same as
399+
// sama dengan
400400
// let double = function(n) { return n * 2 }
401401
*!*
402402
let double = n => n * 2;
@@ -405,17 +405,17 @@ let double = n => n * 2;
405405
alert( double(3) ); // 6
406406
```
407407

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):
409409

410410
```js run
411411
let sayHi = () => alert("Hello!");
412412
413413
sayHi();
414414
```
415415

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

418-
For instance, here's the rewritten example with `welcome()`:
418+
Misalnya, ini contoh yang ditulis ulang dengan `welcome()`:
419419

420420
```js run
421421
let age = prompt("What is your age?", 18);
@@ -427,48 +427,48 @@ let welcome = (age < 18) ?
427427
welcome(); // ok now
428428
```
429429

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

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

434-
```smart header="Multiline arrow functions"
434+
```smart header="Fungsi panah baris-ganda"
435435
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.
437437
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.
439439
440-
Like this:
440+
Seperti ini:
441441
442442
```js run
443-
let sum = (a, b) => { // the curly brace opens a multiline function
443+
let sum = (a, b) => { // kurung kurawal membuka fungsi baris-ganda
444444
let result = a + b;
445445
*!*
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
447447
*/!*
448448
};
449449

450450
alert( sum(1, 2) ); // 3
451451
```
452452

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>.
455455
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.
457457
```
458458

459-
## Summary
459+
## Kesimpulan
460460

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

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

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

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

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

Comments
 (0)