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
There's one more way to create a function. It's rarely used, but sometimes there's no alternative.
4
+
Terdapat satu lagi cara untuk membuat fungsi. Ini sangat jarang digunakan, tapi terkadang tidak ada alternatif lain.
5
5
6
-
## Syntax
6
+
## Sintaks
7
7
8
-
The syntax for creating a function:
8
+
Sintaks untuk membuat sebuah fungsi:
9
9
10
10
```js
11
11
let func =newFunction ([arg1, arg2, ...argN], functionBody);
12
12
```
13
13
14
-
The function is created with the arguments `arg1...argN`and the given`functionBody`.
14
+
Fungsinya dibuat dengan argumen-argumen `arg1...argN`dan diberikan`functionBody`.
15
15
16
-
It's easier to understand by looking at an example. Here's a function with two arguments:
16
+
Ini sangat mudah dimengerti hanya dengan melihat contohnya. Disini sebuah fungsi dengan dua argumen:
17
17
18
18
```js run
19
19
let sum =newFunction('a', 'b', 'return a + b');
20
20
21
21
alert( sum(1, 2) ); // 3
22
22
```
23
23
24
-
And here there's a function without arguments, with only the function body:
24
+
Dan ini fungsi tanpa argumen, hanya ada body dari fungsinya:
25
25
26
26
```js run
27
27
let sayHi =newFunction('alert("Hello")');
28
28
29
29
sayHi(); // Hello
30
30
```
31
31
32
-
The major difference from other ways we've seen is that the function is created literally from a string, that is passed at runtime.
32
+
Perbedaan utama dari cara lain yang pernah kita lihat adalah fungsinya dibuat secara harfiah dari sebuah string, yang dilanjutkan ke run-time.
33
33
34
-
All previous declarations required us, programmers, to write the function code in the script.
34
+
Seluruh deklarasi sebelumnya membutuhkan kita, programmer, untuk menulis kode fungsi didalam skrip.
35
35
36
-
But`new Function`allows to turn any string into a function. For example, we can receive a new function from a server and then execute it:
36
+
Tapi`new Function`mengijinkan kita untuk mengubah string apapun menjadi fungsi. Contoh, kita bisa menerima sebuah fungsi baru dari server dan mengeksekusinya:
37
37
38
38
```js
39
39
let str =... receive the code from a server dynamically ...
@@ -42,15 +42,15 @@ let func = new Function(str);
42
42
func();
43
43
```
44
44
45
-
It is used in very specific cases, like when we receive code from a server, or to dynamically compile a function from a template, in complex web-applications.
45
+
Itu digunakan dalam beberapa kasus yang spesifik, seperti ketika kita menerima kode dari server, atau secara dinamis mengkompilasikan sebuah fungsi dari sebuah template didalam aplikasi-web yang kompleks.
46
46
47
47
## Closure
48
48
49
-
Usually, a function remembers where it was born in the special property `[[Environment]]`. It references the Lexical Environment from where it's created (we covered that in the chapter<info:closure>).
49
+
Biasanya, sebuah fungsi mengingat dimana dirinya dibuat didalam properti spesial `[[Environtment]]. Fungsi itu akan mereferensi lingkungan leksikal dari dimana ia dibuat (kita telah membahasnya didalam bab<info:closure>).
50
50
51
-
But when a function is created using `new Function`, its `[[Environment]]`is set to reference not the current Lexical Environment, but the global one.
51
+
Tapi ketika sebuah fungsi dibuat menggunakan `new Function`, `[[Environment]]`miliknya disetel bukan pada lingkungan leksikal saat ini, tapi pada yang global.
52
52
53
-
So, such function doesn't have access to outer variables, only to the global ones.
53
+
Jadi, fungsi seperti itu tidak memiliki akses kepada variabel luar, hanya pada yang global saja.
54
54
55
55
```js run
56
56
functiongetFunc() {
@@ -66,7 +66,7 @@ function getFunc() {
66
66
getFunc()(); // error: value is not defined
67
67
```
68
68
69
-
Compare it with the regular behavior:
69
+
Bandingkan dengan yang biasa:
70
70
71
71
```js run
72
72
functiongetFunc() {
@@ -79,45 +79,45 @@ function getFunc() {
79
79
return func;
80
80
}
81
81
82
-
getFunc()(); // *!*"test"*/!*, from the Lexical Environment of getFunc
82
+
getFunc()(); // *!*"test"*/!*, berasal dari lingkungan leksikal dari getFung
83
83
```
84
84
85
-
This special feature of `new Function`looks strange, but appears very useful in practice.
85
+
Fitur spesial dari `new Function`ini terlihat aneh, tapi akan sangat berguna didalam penerapannya.
86
86
87
-
Imagine that we must create a function from a string. The code of that function is not known at the time of writing the script (that's why we don't use regular functions), but will be known in the process of execution. We may receive it from the server or from another source.
87
+
Bayangkan kalau kita harus membuat sebuah fungsi dari string. Kode dari fungsinya tidak diketahui saat penulisan skrip (itulah kenapa kita tidak menggunakan fungsi yang biasa), tapi akan diketahui saat proses dari eksekusinya. Kita mungkin menerima kodenya itu dari server atau sumber lainnya.
88
88
89
-
Our new function needs to interact with the main script.
89
+
Fungsi baru kita membutuhkan interaksi dengan skrip utamanya.
90
90
91
-
What if it could access the outer variables?
91
+
Bagaimana jika itu bisa mengakses variabel luar?
92
92
93
-
The problem is that before JavaScript is published to production, it's compressed using a*minifier* -- a special program that shrinks code by removing extra comments, spaces and -- what's important, renames local variables into shorter ones.
93
+
Masalahnya adalah saat Javascript belum dipublikasikan untuk produksi, itu akan dikompresi menggunakan*minifier* -- sebuah program spesial yang mengecilkan ukuran kode dengan menghapus komentar-komentar, spasi dan -- yang paling penting, menamai variabel lokal menjadi lebih pendek.
94
94
95
-
For instance, if a function has`let userName`, minifier replaces it `let a` (or another letter if this one is occupied), and does it everywhere. That's usually a safe thing to do, because the variable is local, nothing outside the function can access it. And inside the function, minifier replaces every mention of it. Minifiers are smart, they analyze the code structure, so they don't break anything. They're not just a dumb find-and-replace.
95
+
Contoh, jika sebuah fungsi mempunyai`let userName`, minifier akan mengganti itu dengan `let a` (atau huruf lainnya jika tidak hurufnya tidak tersedia), dan melakukannya dimanapun. Sebenarnya itu adalah yang yang aman untuk dilakukan, karena variabelnya lokal, tidak ada sesuatu dari luar fungsinya yang bisa mengaksesnya. Dan didalam fungsinya, minifier mengganti seluruh penamaan variabelnya. Minifier cukup pintar, mereka menganalisa struktur kodenya, jadi mereka tidak akan merusak apapun. Minifier bukanlah hal bodoh yang hanya akan mencari-dan-mengganti.
96
96
97
-
So if`new Function`had access to outer variables, it would be unable to find renamed `userName`.
97
+
Jadi jika`new Function`mempunyai akses ke variabel luar, itu tidak akan bisa menemukan `userName` yang telah dinamai ulang.
98
98
99
-
**If`new Function`had access to outer variables, it would have problems with minifiers.**
99
+
**Jika`new Function`mempunyai akses ke variabel luar, itu akan membuat masalah dengan minifiernya..**
100
100
101
-
Besides, such code would be architecturally bad and prone to errors.
101
+
Selain itu, kode seperti itu secara arsitekturnya jelek dan rentan terhadap error.
102
102
103
-
To pass something to a function, created as`new Function`, we should use its arguments.
103
+
Untuk memberikan sesuatu kepada fungsi, dibuat sebagai`new Function`, kita seharusnya menggunakan argumennya.
104
104
105
-
## Summary
105
+
## Ringkasan
106
106
107
-
The syntax:
107
+
Sintaks:
108
108
109
109
```js
110
110
let func =newFunction ([arg1, arg2, ...argN], functionBody);
111
111
```
112
112
113
-
For historical reasons, arguments can also be given as a comma-separated list.
113
+
Untuk sebuah alasan lama, argumen-argumen bisa diberikan sebagai daftar dengan koma.
114
114
115
-
These three declarations mean the same:
115
+
Ketiga deklarasi ini melakukan hal yang sama:
116
116
117
117
```js
118
-
newFunction('a', 'b', 'return a + b'); //basic syntax
119
-
newFunction('a,b', 'return a + b'); //comma-separated
120
-
newFunction('a , b', 'return a + b'); //comma-separated with spaces
118
+
newFunction('a', 'b', 'return a + b'); //sintaks dasar
119
+
newFunction('a,b', 'return a + b'); //dipisahkan dengan kona
120
+
newFunction('a , b', 'return a + b'); //dipisahkan dengan koma dan ditambah spasi
121
121
```
122
122
123
-
Functions created with `new Function`, have`[[Environment]]`referencing the global Lexical Environment, not the outer one. Hence, they cannot use outer variables. But that's actually good, because it insures us from errors. Passing parameters explicitly is a much better method architecturally and causes no problems with minifiers.
123
+
Fungsi yang dibuat dengan `new Function`, memiliki`[[Environment]]`mereferensi kepada lingkungan leksikal global, bukan bagian luarnya. Karenanya, mereka tidak bisa menggunakan variabel di bagian luarnya. Tapi sebenarnya itu bagus karena itu memastikan kita menjauh dari error. Memberikan parameter secara jelas adalah metode yang lebih baik secara arsitektur dan tidak akan menyebabkan error pada minifier.
0 commit comments