Skip to content

Eval: run a code string #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
15d0c57
Update article.md
HakaCode Jul 31, 2020
d0c9961
Merge pull request #1 from HakaCode/master
HakaCode Jul 31, 2020
3e66630
Revert "Update article.md"
HakaCode Jul 31, 2020
aa9bf40
Fix things and translate the summary of eval article
Jul 31, 2020
8de0cf3
Update article.md
Jul 31, 2020
e1237c2
Update article.md
Jul 31, 2020
514360d
Update article.md
Jul 31, 2020
e53e33a
Update task.md
Jul 31, 2020
ea04503
Merge branch 'master' into revert-1-master
Aug 1, 2020
bc970cb
Merge pull request #2 from ashuradev/revert-1-master
Aug 1, 2020
462fe29
Update article.md
HakaCode Aug 1, 2020
674bdd4
Merge pull request #3 from HakaCode/master
Aug 1, 2020
be6157f
Update article.md
HakaCode Aug 1, 2020
cb7e241
Update article.md
Aug 1, 2020
65ee8f0
Merge branch 'master' into fix-conflicts
Jan 31, 2021
b3ebc0d
Update 1-js/99-js-misc/02-eval/1-eval-calculator/task.md
May 19, 2021
dea981d
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
627898f
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
5589be2
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
47c9e89
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
9385f54
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
80feedb
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
e1cca32
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
980f579
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
75371f9
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
32ce693
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
05dd276
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
3585e5a
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
7680f57
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
cf4d973
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
26ffb20
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
1f002e2
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
3cc8dab
Update 1-js/99-js-misc/02-eval/article.md
May 19, 2021
2c95a88
Update article.md
odsantos May 19, 2021
2bf304a
Update article.md
odsantos May 19, 2021
8756ef0
Add solution translation
odsantos Jan 20, 2023
3691f56
Merge branch 'master' into fix-conflicts
odsantos Jan 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 1-js/99-js-misc/02-eval/1-eval-calculator/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ importance: 4

---

# Eval-calculator
# Calculadora-eval

Create a calculator that prompts for an arithmetic expression and returns its result.
Crie uma calculadora que solicite uma expressão aritmética e retorne seu resultado.

There's no need to check the expression for correctness in this task. Just evaluate and return the result.
Não há necessidade de verificar a expressão para correção nesta tarefa. Apenas avalie e retorne o resultado.

[demo]
52 changes: 26 additions & 26 deletions 1-js/99-js-misc/02-eval/article.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
# Eval: run a code string

The built-in `eval` function allows to execute a string of code.
A função interna `eval` permite executar uma string de código.

The syntax is:
A sintaxe é:

```js
let result = eval(code);
let resultado = eval(codigo);
```

For example:
Por exemplo:

```js run
let code = 'alert("Hello")';
eval(code); // Hello
let codigo = 'alert("Olá")';
eval(codigo); // Olá
```

A string of code may be long, contain line breaks, function declarations, variables and so on.
A string código pode ser longa, conter quebras de linha, declarações de função, variáveis e assim por diante.

The result of `eval` is the result of the last statement.
O resultado de `eval` é o resultado da última declaração.

For example:
Por exemplo:
```js run
let value = eval('1+1');
alert(value); // 2
let valor = eval('1+1');
alert(valor); // 2
```

```js run
let value = eval('let i = 0; ++i');
alert(value); // 1
alert(valor); // 1
```

The eval'ed code is executed in the current lexical environment, so it can see outer variables:
O código avaliado é executado no ambiente lexical atual, para que ele possa ver variáveis externas:

```js run no-beautify
let a = 1;
Expand All @@ -46,26 +46,26 @@ function f() {
f();
```

It can change outer variables as well:
Também pode alterar variáveis externas:

```js untrusted refresh run
let x = 5;
eval("x = 10");
alert(x); // 10, value modified
alert(x); // 10, valor modificado
```

In strict mode, `eval` has its own lexical environment. So functions and variables, declared inside eval, are not visible outside:
No modo estrito, o `eval` possui seu próprio ambiente lexical. Portanto, funções e variáveis, declaradas dentro de eval, não são visíveis fora:

```js untrusted refresh run
// reminder: 'use strict' is enabled in runnable examples by default
// lembrete: 'use strict' está ativado em exemplos executáveis por padrão

eval("let x = 5; function f() {}");

alert(typeof x); // undefined (no such variable)
// function f is also not visible
alert(typeof x); // undefined (não tal variável)
// a função f também não é visível
```

Without `use strict`, `eval` doesn't have its own lexical environment, so we would see `x` and `f` outside.
Sem `use strict`,` eval` não possui seu próprio ambiente lexical, portanto veríamos `x` e` f` fora.

## Using "eval"

Expand Down Expand Up @@ -105,10 +105,10 @@ f(5); // 5

The `new Function` construct is explained in the chapter <info:new-function>. It creates a function from a string, also in the global scope. So it can't see local variables. But it's so much clearer to pass them explicitly as arguments, like in the example above.

## Summary
## Resumo

A call to `eval(code)` runs the string of code and returns the result of the last statement.
- Rarely used in modern JavaScript, as there's usually no need.
- Can access outer local variables. That's considered bad practice.
- Instead, to `eval` the code in the global scope, use `window.eval(code)`.
- Or, if your code needs some data from the outer scope, use `new Function` and pass it as arguments.
Uma chamada para `eval(codigo)` executa a string de código e retorna o resultado da última instrução.
- Raramente usado no JavaScript moderno, pois geralmente não há necessidade.
- Pode acessar variáveis locais externas. Isso é considerado uma má prática.
- Em vez disso, para `eval` o código no escopo global, use `window.eval(codigo)`.
- Ou, se o seu código precisar de alguns dados do escopo externo, use `new Function` e passe-os como argumentos.