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
+40-40Lines changed: 40 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,69 +1,69 @@
1
-
# Function expressions and arrows
1
+
# Expresi fungsi dan panah
2
2
3
-
In JavaScript, a function is not a "magical language structure", but a special kind of value.
3
+
Di JavaScript, fungsi bukan "struktur bahasa magis", melaikan satu bentuk nilai spesial.
4
4
5
-
The syntax that we used before is called a *Function Declaration*:
5
+
Syntax yang kita gunakan sebelumnya disebut *Deklarasi Fungsi*:
6
6
7
7
```js
8
8
functionsayHi() {
9
9
alert( "Hello" );
10
10
}
11
11
```
12
12
13
-
There is another syntax for creating a function that is called a *Function Expression*.
13
+
Ada syntax lain untuk membuat fungsi yang disebut *Expresi Fungsi*.
14
14
15
-
It looks like this:
15
+
Rupanya seperti ini:
16
16
17
17
```js
18
18
letsayHi=function() {
19
19
alert( "Hello" );
20
20
};
21
21
```
22
22
23
-
Here, the function is created and assigned to the variable explicitly, like any other value. No matter how the function is defined, it's just a value stored in the variable`sayHi`.
23
+
Di sini, fungsi dibuat dan diisi variabel secara explisit, seperti nilai lain manapun. Tak peduli bagaimana fungsi didefinisi, ia hanya suatu nilai yang disimpan dalam variabel`sayHi`.
24
24
25
-
The meaning of these code samples is the same: "create a function and put it into the variable`sayHi`".
25
+
Arti dari sampel kode ini sama: "buatlah fungs dan taruhlah di dalam variabel`sayHi`".
26
26
27
-
We can even print out that value using`alert`:
27
+
Kita bahkan bisa mencetak nilai itu menggunakan`alert`:
28
28
29
29
```js run
30
30
functionsayHi() {
31
31
alert( "Hello" );
32
32
}
33
33
34
34
*!*
35
-
alert( sayHi ); //shows the function code
35
+
alert( sayHi ); //menampilkan kode fungsi
36
36
*/!*
37
37
```
38
38
39
-
Please note that the last line does not run the function, because there are no parentheses after`sayHi`. There are programming languages where any mention of a function name causes its execution, but JavaScript is not like that.
39
+
Tolong ingat bahwa baris terakhir tidak menjalankan fungsi, karena tak ada tanda kurung setelah`sayHi`. Ada bahasa pemrograman di mana satu penyebutan nama fungsi menyebabkan exekusi fungsi, tapi JavaScript tak seperti itu.
40
40
41
-
In JavaScript, a function is a value, so we can deal with it as a value. The code above shows its string representation, which is the source code.
41
+
Di JavaScript, fungsi adalah nilai, jadi kita bisa menghadapinya sebagai nilai. Kode di atas menunjukkan representasi stringnya, yang mana kode sumbernya.
42
42
43
-
Surely, a function is a special values, in the sense that we can call it like`sayHi()`.
43
+
Pastinya, fungsi adalah nilai spesial, dengan anggapan bahwa kita bisa memanggilnya seperti`sayHi()`.
44
44
45
-
But it's still a value. So we can work with it like with other kinds of values.
45
+
Tapi ia tetaplah nilai. Jadi kita bisa memakainya seperti macam nilai lainnya.
46
46
47
-
We can copy a function to another variable:
47
+
Kita bisa mengkopi fungsi ke variabel lain:
48
48
49
49
```js run no-beautify
50
-
functionsayHi() { // (1) create
50
+
functionsayHi() { // (1) buat
51
51
alert( "Hello" );
52
52
}
53
53
54
-
let func = sayHi; // (2) copy
54
+
let func = sayHi; // (2) kopi
55
55
56
-
func(); // Hello // (3) run the copy (it works)!
57
-
sayHi(); // Hello // this still works too (why wouldn't it)
sayHi(); // Hello // ini juga masih bekerja (kenapa tidak)
58
58
```
59
59
60
-
Here's what happens above in detail:
60
+
Inilah yang terjadi di atas secara detil:
61
61
62
-
1.The Function Declaration `(1)`creates the function and puts it into the variable named`sayHi`.
63
-
2.Line`(2)`copies it into the variable `func`. Please note again: there are no parentheses after`sayHi`. If there were, then`func = sayHi()`would write*the result of the call*`sayHi()`into`func`, not *the function*`sayHi`itself.
64
-
3.Now the function can be called as both `sayHi()`and`func()`.
62
+
1.Deklarasi Fungsi `(1)`membuat fungsi dan menaruhnya ke variabel bernama`sayHi`.
63
+
2.Baris`(2)`mengkopinya ke variabel `func`. Tolong ingat lagi: tak ada tanda kurung setelah`sayHi`. Jika ada, maka`func = sayHi()`akan menulis*hasil panggilan*`sayHi()`ke`func`, bukan *fungsi*`sayHi`itu sendiri.
64
+
3.Sekarang fungsi bisa dipanggil baik sebagai `sayHi()`maupun`func()`.
65
65
66
-
Note that we could also have used a Function Expression to declare `sayHi`, in the first line:
66
+
Catat bahwa kita jusa bisa menggunakan Expresi Fungsi untuk mendeklarasi `sayHi`, di baris pertama:
67
67
68
68
```js
69
69
letsayHi=function() {
@@ -74,11 +74,11 @@ let func = sayHi;
74
74
// ...
75
75
```
76
76
77
-
Everything would work the same.
77
+
Semua akan berjalan sama.
78
78
79
79
80
-
````smart header="Why is there a semicolon at the end?"
81
-
You might wonder, why does Function Expression have a semicolon `;` at the end, but Function Declaration does not:
80
+
````smart header="Kenapa ada semicolon di akhir?"
81
+
Kamu mungkin penasaran, kenapa Expresi Fungsi punya semicolon `;` di akhir, tapi Deklarasi Fungsi tidak:
82
82
83
83
```js
84
84
function sayHi() {
@@ -90,27 +90,27 @@ let sayHi = function() {
90
90
}*!*;*/!*
91
91
```
92
92
93
-
The answer is simple:
94
-
- There's no need for `;` at the end of code blocks and syntax structures that use them like `if { ... }`, `for { }`, `function f { }` etc.
95
-
- A Function Expression is used inside the statement: `let sayHi = ...;`, as a value. It's not a code block, but rather an assignment. The semicolon `;` is recommended at the end of statements, no matter what the value is. So the semicolon here is not related to the Function Expression itself, it just terminates the statement.
93
+
Jawabannya simpel:
94
+
- `;` tidak dibutuhkan di akhir blok kode dan struktur syntax yang memakai mereka seperti `if { ... }`, `for { }`, `function f { }` dll.
95
+
- Expresi Fungsi digunakan di dalam pernyataan: `let sayHi = ...;`, sebagai nilai. Ia bukan blok kode, tapi lebih ke penetapan. Semicolon `;` disarankan di akhir pernyataan, tak peduli nilainya apa. Jadi semicolon di sini tak ada hubungannya dengan Expresi Fungsi itu sendiri, ia hanya menstop pernyataan.
96
96
````
97
97
98
-
## Callback functions
98
+
## Fungsi callback
99
99
100
-
Let's look at more examples of passing functions as values and using function expressions.
100
+
Ayo kita lihat pada contoh lain mengoper fungsi sebagai nilai dan menggunakan expresi fungsi.
101
101
102
-
We'll write a function`ask(question, yes, no)`with three parameters:
102
+
Kita akan menulis fungsi`ask(question, yes, no)`dengan tiga parameter:
103
103
104
104
`question`
105
-
: Text of the question
105
+
: Teks pertanyaan
106
106
107
107
`yes`
108
-
: Function to run if the answer is "Yes"
108
+
: Fungsi yang berjalan jika jawabannya "Yes"
109
109
110
110
`no`
111
-
: Function to run if the answer is "No"
111
+
: Fungsi yang berjalan jika jawabannya "No"
112
112
113
-
The function should ask the `question`and, depending on the user's answer, call`yes()`or`no()`:
113
+
Fungsinya akan menanyakan `question`dan, tergantung jawabannya pengguna, panggil`yes()`atau`no()`:
114
114
115
115
```js run
116
116
*!*
@@ -132,13 +132,13 @@ function showCancel() {
132
132
ask("Do you agree?", showOk, showCancel);
133
133
```
134
134
135
-
In practice, such functions are quite useful. The major difference between a real-life `ask`and the example above is that real-life functions use more complex ways to interact with the user than a simple `confirm`. In the browser, such function usually draws a nice-looking question window. But that's another story.
135
+
Pada praktiknya, fungsi macam ini agak berguna. Perbedaan besar antara `ask`kehidupan-nyata dan contoh di atas adalah fungsi kehidupan-nyata memakai cara komplex untuk berinteraksi dengan pengguna daripada sekedar `confirm`. Di peramban, fungsi macam ini biasanya menarik window pertanyaan menarik. Tapi itu cerita lain lagi.
136
136
137
-
**The arguments `showOk`and`showCancel`of`ask`are called *callback functions* or just *callbacks*.**
137
+
**Argumen `showOk`dan`showCancel`dari`ask`dipanggil *fungsi callback* atau hanya *callback*.**
138
138
139
-
The idea is that we pass a function and expect it to be "called back" later if necessary. In our case, `showOk`becomes the callback for "yes" answer, and`showCancel`for "no" answer.
139
+
Idenya adalah kita mengoper fungsi dan berharap ia "dipanggil kembali" kemudian jika dibutuhkan. Pada kasus kita, `showOk`menjadi callback untuk jawaban "yes", dan`showCancel`untuk jawaban "no".
140
140
141
-
We can use Function Expressions to write the same function much shorter:
141
+
Kita bisa memakai Expresi Fungsi untuk menulis fungsi yang sama lebih pendek:
0 commit comments