Skip to content

Commit 8fc6d38

Browse files
committed
docs: translate the first two section of call-apply-decorators/article.md
1 parent 5a2cc59 commit 8fc6d38

File tree

1 file changed

+25
-25
lines changed
  • 1-js/06-advanced-functions/09-call-apply-decorators

1 file changed

+25
-25
lines changed

1-js/06-advanced-functions/09-call-apply-decorators/article.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
# Decorators and forwarding, call/apply
1+
# Decoradores e reencaminhamento, chamar/aplicar
22

3-
JavaScript gives exceptional flexibility when dealing with functions. They can be passed around, used as objects, and now we'll see how to *forward* calls between them and *decorate* them.
3+
A JavaScript oferece uma flexibilidade excecional ao lidar com funções. Podem ser transmitidas, utilizadas como objetos, e agora veremos como *encaminhar* chamadas entre elas e *decorá-las*.
44

5-
## Transparent caching
5+
## Memorização transparente
66

7-
Let's say we have a function `slow(x)` which is CPU-heavy, but its results are stable. In other words, for the same `x` it always returns the same result.
7+
Digamos que temos uma função `slow(x)` que consume muito processamento, mas seus resultados são estáveis. Em outras palavras, para o mesmo `x` esta sempre retorna o mesmo resultado.
88

9-
If the function is called often, we may want to cache (remember) the results to avoid spending extra-time on recalculations.
9+
Se a função for chamada com frequência, podemos querer memorizar (lembrar) os resultados para evitar gastar tempo adicional em recálculos.
1010

11-
But instead of adding that functionality into `slow()` we'll create a wrapper function, that adds caching. As we'll see, there are many benefits of doing so.
11+
Mas, em vez de adicionar essa funcionalidade em `slow()` criaremos uma função de embrulho, que adiciona a memorização. Como veremos, existem muitas vantagens em fazê-lo.
1212

13-
Here's the code, and explanations follow:
13+
Eis o código, e as explicações que se seguem:
1414

1515
```js run
1616
function slow(x) {
17-
// there can be a heavy CPU-intensive job here
17+
// pode haver um processamento intensivo aqui
1818
alert(`Called with ${x}`);
1919
return x;
2020
}
@@ -23,43 +23,43 @@ function cachingDecorator(func) {
2323
let cache = new Map();
2424

2525
return function(x) {
26-
if (cache.has(x)) { // if there's such key in cache
27-
return cache.get(x); // read the result from it
26+
if (cache.has(x)) { // se existir tal chave na memória
27+
return cache.get(x); // ler o resultado do mesmo
2828
}
2929

30-
let result = func(x); // otherwise call func
30+
let result = func(x); // caso contrário, chamar função
3131

32-
cache.set(x, result); // and cache (remember) the result
32+
cache.set(x, result); // e memorizar (lembrar) o resultado
3333
return result;
3434
};
3535
}
3636

3737
slow = cachingDecorator(slow);
3838

39-
alert( slow(1) ); // slow(1) is cached and the result returned
40-
alert( "Again: " + slow(1) ); // slow(1) result returned from cache
39+
alert( slow(1) ); // slow(1) é memorizado e o resultado retornado
40+
alert( "Again: " + slow(1) ); // resultado da slow(1) retornado da memória
4141

42-
alert( slow(2) ); // slow(2) is cached and the result returned
43-
alert( "Again: " + slow(2) ); // slow(2) result returned from cache
42+
alert( slow(2) ); // slow(2) é memorizado e o resultado retornado
43+
alert( "Again: " + slow(2) ); // resultado da slow(2) retornado da memória
4444
```
4545

46-
In the code above `cachingDecorator` is a *decorator*: a special function that takes another function and alters its behavior.
46+
No código acima `cachingDecorator` é um *decorador*: uma função especial que pega em outra função e altera o seu comportamento.
4747

48-
The idea is that we can call `cachingDecorator` for any function, and it will return the caching wrapper. That's great, because we can have many functions that could use such a feature, and all we need to do is to apply `cachingDecorator` to them.
48+
A ideia é que podemos chamar `cachingDecorator` para qualquer função, e esta retornará o embrulho de memorização. Isto é ótimo, porque podemos ter muitas funções que poderiam usar esse recurso, e tudo o que precisamos fazer é aplicar `cachingDecorator` a elas.
4949

50-
By separating caching from the main function code we also keep the main code simpler.
50+
Ao separar a memorização do código da função principal, também mantemos o código principal mais simples.
5151

52-
The result of `cachingDecorator(func)` is a "wrapper": `function(x)` that "wraps" the call of `func(x)` into caching logic:
52+
O resultado de `caching` é um "embrulho": `function(x)` que "embrulha" a chama da `func(x)` na lógica de memorização:
5353

5454
![](decorator-makecaching-wrapper.svg)
5555

56-
From an outside code, the wrapped `slow` function still does the same. It just got a caching aspect added to its behavior.
56+
A partir dum código externo, a função `slow` embrulhada ainda faz o mesmo. Esta apenas tem um aspeto de memorização adicionado ao seu comportamento.
5757

58-
To summarize, there are several benefits of using a separate `cachingDecorator` instead of altering the code of `slow` itself:
58+
Para resumir, existem vários benefícios em utilizar um `cachingDecorator` separado ao invés de alterar o código da `slow` em si:
5959

60-
- The `cachingDecorator` is reusable. We can apply it to another function.
61-
- The caching logic is separate, it did not increase the complexity of `slow` itself (if there was any).
62-
- We can combine multiple decorators if needed (other decorators will follow).
60+
- O `cachingDecorator` é reutilizável. Podemos aplicá-lo a outra função.
61+
- A lógica de memorização é separada, não aumentou a complexidade da `slow` em si (se é que existia alguma).
62+
- Podemos combinar vários decoradores, se necessário (outros decoradores seguir-se-ão).
6363

6464
## Using "func.call" for the context
6565

0 commit comments

Comments
 (0)