Skip to content

Commit 1d7566f

Browse files
authored
Update article.md
1 parent c77fadf commit 1d7566f

File tree

1 file changed

+15
-15
lines changed
  • 1-js/06-advanced-functions/07-new-function

1 file changed

+15
-15
lines changed

1-js/06-advanced-functions/07-new-function/article.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,42 @@ let func = new Function ([arg1[, arg2[, ...argN]],] functionBody)
1313

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

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

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

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

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

2626
```js run
27-
let sayHi = new Function('alert("Hello")');
27+
let sayHi = new Function('alert("Olá")');
2828

29-
sayHi(); // Hello
29+
sayHi(); // Olá
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+
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.
3333

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`.
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+
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:
3737

3838
```js
39-
let str = ... receive the code from a server dynamically ...
39+
let str = ... recebe o código de um servidor dinamicamente ...
4040

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

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

5353
```js run
5454

@@ -62,10 +62,10 @@ function getFunc() {
6262
return func;
6363
}
6464

65-
getFunc()(); // error: value is not defined
65+
getFunc()(); // erro: value não foi definido
6666
```
6767

68-
Compare it with the regular behavior:
68+
Compare-a com o comportamento padrão:
6969

7070
```js run
7171
function getFunc() {
@@ -78,10 +78,10 @@ function getFunc() {
7878
return func;
7979
}
8080

81-
getFunc()(); // *!*"test"*/!*, from the Lexical Environment of getFunc
81+
getFunc()(); // *!*"test"*/!*, do escopo léxico de getFunc
8282
```
8383

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

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

0 commit comments

Comments
 (0)