Skip to content

Commit 584a346

Browse files
committed
Translation
Translation js/advanced-functions/new-function/article
1 parent 5b80dec commit 584a346

1 file changed

Lines changed: 34 additions & 34 deletions

File tree

  • 1-js/06-advanced-functions/07-new-function
Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11

2-
# The "new Function" syntax
2+
# Sintaks "new Function"
33

4-
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.
55

6-
## Syntax
6+
## Sintaks
77

8-
The syntax for creating a function:
8+
Sintaks untuk membuat sebuah fungsi:
99

1010
```js
1111
let func = new Function ([arg1, arg2, ...argN], functionBody);
1212
```
1313

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`.
1515

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

1818
```js run
1919
let sum = new Function('a', 'b', 'return a + b');
2020

2121
alert( sum(1, 2) ); // 3
2222
```
2323

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

2626
```js run
2727
let sayHi = new Function('alert("Hello")');
2828

2929
sayHi(); // Hello
3030
```
3131

32-
The major difference from other ways we've seen is that the function is created literally from a string, that is passed at run time.
32+
Perbedaan utama dari cara lain yang pernah kita lihat adalah fungsinya dibuat secara harfiah dari sebuah string, yang dilanjutkan ke run-time.
3333

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

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

3838
```js
3939
let str = ... receive the code from a server dynamically ...
@@ -42,15 +42,15 @@ let func = new Function(str);
4242
func();
4343
```
4444

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

4747
## Closure
4848

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>).
5050

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

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

5555
```js run
5656
function getFunc() {
@@ -66,7 +66,7 @@ function getFunc() {
6666
getFunc()(); // error: value is not defined
6767
```
6868

69-
Compare it with the regular behavior:
69+
Bandingkan dengan yang biasa:
7070

7171
```js run
7272
function getFunc() {
@@ -79,45 +79,45 @@ function getFunc() {
7979
return func;
8080
}
8181

82-
getFunc()(); // *!*"test"*/!*, from the Lexical Environment of getFunc
82+
getFunc()(); // *!*"test"*/!*, berasal dari lingkungan leksikal dari getFung
8383
```
8484

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

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

89-
Our new function needs to interact with the main script.
89+
Fungsi baru kita membutuhkan interaksi dengan skrip utamanya.
9090

91-
What if it could access the outer variables?
91+
Bagaimana jika itu bisa mengakses variabel luar?
9292

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

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

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

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

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

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

105-
## Summary
105+
## Ringkasan
106106

107-
The syntax:
107+
Sintaks:
108108

109109
```js
110110
let func = new Function ([arg1, arg2, ...argN], functionBody);
111111
```
112112

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

115-
These three declarations mean the same:
115+
Ketiga deklarasi ini melakukan hal yang sama:
116116

117117
```js
118-
new Function('a', 'b', 'return a + b'); // basic syntax
119-
new Function('a,b', 'return a + b'); // comma-separated
120-
new Function('a , b', 'return a + b'); // comma-separated with spaces
118+
new Function('a', 'b', 'return a + b'); // sintaks dasar
119+
new Function('a,b', 'return a + b'); // dipisahkan dengan kona
120+
new Function('a , b', 'return a + b'); // dipisahkan dengan koma dan ditambah spasi
121121
```
122122

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

Comments
 (0)