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/06-advanced-functions/07-new-function/article.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,42 +13,42 @@ let func = new Function ([arg1[, arg2[, ...argN]],] functionBody)
13
13
14
14
Em outras palavras, os parâmetros da função (ou, mais precisamente, os nomes deles) vêm primeiro, e o corpo da função vem por último. Todos os argumentos são `strings`.
15
15
16
-
It's easier to understand by looking at an example. Here's a function with two arguments:
16
+
É mais de compreender olhando um exemplo. Aqui está uma função com dois argumentos:
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
-
If there are no arguments, then there's only a single argument, the function body:
24
+
Se não existem argumentos, então somente existe um único argumento, que é o corpo da função:
25
25
26
26
```js run
27
-
let sayHi =newFunction('alert("Hello")');
27
+
let sayHi =newFunction('alert("Olá")');
28
28
29
-
sayHi(); //Hello
29
+
sayHi(); //Olá
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 run time.
32
+
A maior diferença de outros métodos vistos é que a função é literalmente criada a partir de uma `string`, que é passada em tempo de execução.
33
33
34
-
All previous declarations required us, programmers, to write the function code in the script.
34
+
Todas as declarações anteriores requeriam de nós, programadores, escrever o código da função dentro do `script`.
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
+
Mas`new Function`permite transformar qualquer `string` em uma função. Por exemplo, nós podemos receber uma nova função de um servidor e executa-la:
37
37
38
38
```js
39
-
let str =...receive the code from a server dynamically...
39
+
let str =...recebe o código de um servidor dinamicamente...
40
40
41
41
let func =newFunction(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. The need for that usually arises at advanced stages of development.
45
+
Ela é usada em casos muito específicos, como quando nós recebemos código de um servidor, ou para compilar dinamicamente a função a partir de um template. A necessidade disso geralmente surge em estágios avançados de desenvolvimento.
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.
49
+
No geral, uma função se "lembra" de onde ela foi criada na propiedade especial `[[Environment]]`. Ela referencia o escopo léxico de onde ela foi criada.
50
50
51
-
But when a function is created using`new Function`, its `[[Environment]]`references not the current Lexical Environment, but instead the global one.
51
+
Porém quando uma função é criada usando`new Function`, a sua `[[Environment]]`não referencia o atual escopo léxico, mas sim o escopo global.
52
52
53
53
```js run
54
54
@@ -62,10 +62,10 @@ function getFunc() {
62
62
return func;
63
63
}
64
64
65
-
getFunc()(); //error: value is not defined
65
+
getFunc()(); //erro: value não foi definido
66
66
```
67
67
68
-
Compare it with the regular behavior:
68
+
Compare-a com o comportamento padrão:
69
69
70
70
```js run
71
71
functiongetFunc() {
@@ -78,10 +78,10 @@ function getFunc() {
78
78
return func;
79
79
}
80
80
81
-
getFunc()(); // *!*"test"*/!*, from the Lexical Environment of getFunc
81
+
getFunc()(); // *!*"test"*/!*, do escopo léxico de getFunc
82
82
```
83
83
84
-
This special feature of`new Function`looks strange, but appears very useful in practice.
84
+
Essa caracteristica especial de`new Function`parece estranha, mas se apresenta muito útil na prática.
85
85
86
86
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.
0 commit comments