Skip to content
Merged
Changes from 1 commit
Commits
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
50 changes: 25 additions & 25 deletions 1-js/02-first-steps/14-function-basics/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,88 +47,88 @@ Se precisarmos mudar a mensagem ou a maneira que ela é mostrada, basta modifica

## Variáveis locais

A variable declared inside a function is only visible inside that function.
Uma variável declarada dentro de uma função é apenas visível dentro dessa função.

For example:
Por exemplo:

```js run
function showMessage() {
*!*
let message = "Hello, I'm JavaScript!"; // local variable
let message = "Olá, Eu sou JavaScript!"; // variável local
*/!*

alert( message );
}

showMessage(); // Hello, I'm JavaScript!
showMessage(); // Olá, Eu sou JavaScript!

alert( message ); // <-- Error! The variable is local to the function
alert( message ); // <-- Erro! A variável é local para a função
```

## Outer variables
## Variáveis externas

A function can access an outer variable as well, for example:
Uma função também pode acessar uma variável externa, por exemplo:

```js run no-beautify
let *!*userName*/!* = 'John';

function showMessage() {
let message = 'Hello, ' + *!*userName*/!*;
let message = 'Olá, ' + *!*userName*/!*;
alert(message);
}

showMessage(); // Hello, John
showMessage(); // Olá, John
```

The function has full access to the outer variable. It can modify it as well.
A função tem acesso total à variável externa. Pode modificá-la também.

For instance:
Por exemplo:

```js run
let *!*userName*/!* = 'John';

function showMessage() {
*!*userName*/!* = "Bob"; // (1) changed the outer variable
*!*userName*/!* = "Bob"; // (1) mudou a variável externa

let message = 'Hello, ' + *!*userName*/!*;
let message = 'Olá, ' + *!*userName*/!*;
alert(message);
}

alert( userName ); // *!*John*/!* before the function call
alert( userName ); // *!*John*/!* antes da chamada da função

showMessage();

alert( userName ); // *!*Bob*/!*, the value was modified by the function
alert( userName ); // *!*Bob*/!*, o valor foi modificado pela função
```

The outer variable is only used if there's no local one. So an occasional modification may happen if we forget `let`.
A variável externa é apenas usada se não existir uma local. Então, uma modificação ocasional pode acontencer se esquercermos do `let`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • "...pode acontencer acontecer se esquercermos esquecermos do..."


If a same-named variable is declared inside the function then it *shadows* the outer one. For instance, in the code below the function uses the local `userName`. The outer one is ignored:
Se uma variável com o mesmo nome é declarada dentro da função, então ela *shadows* a externa. Por exemplo, no código abaixo, a função usa o `userName` local. O exterior é ignorado:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • "...então ela encobre (shadows) a externa."
    (encobre e shadows in italics)


```js run
let userName = 'John';

function showMessage() {
*!*
let userName = "Bob"; // declare a local variable
let userName = "Bob"; // declara uma variável local
*/!*

let message = 'Hello, ' + userName; // *!*Bob*/!*
let message = 'Olá, ' + userName; // *!*Bob*/!*
alert(message);
}

// the function will create and use its own userName
// a função criará e usará seu próprio userName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • "...usará o seu próprio..."

showMessage();

alert( userName ); // *!*John*/!*, unchanged, the function did not access the outer variable
alert( userName ); // *!*John*/!*, inalterado, a função não acessou a variável externa
```

```smart header="Global variables"
Variables declared outside of any function, such as the outer `userName` in the code above, are called *global*.
```smart header="Variáveis globais"
Variáveis declaradas fora de qualquer função, como o `userName` externo no código acima, são chamados de *globais*.

Global variables are visible from any function (unless shadowed by locals).
Variáveis globais são visíveis por qualquer função (a não ser que sejam sombreadas pelas locais).

Usually, a function declares all variables specific to its task. Global variables only store project-level data, and it's important that these variables are accessible from anywhere. Modern code has few or no globals. Most variables reside in their functions.
Normalmente, uma função declara todas as variáveis específicas de sua tarefa. As variáveis globais armazenam apenas dados em nível de projeto e é importante que essas variáveis sejam acessíveis de qualquer lugar. Código moderno tem poucos ou nenhum globais. A maioria das variáveis reside em suas funções.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • "...específicas de para a sua tarefa."
  • "...dados em a nível de projeto..."
  • "...poucos poucas ou nenhum nenhumas globais."

```

## Parameters
Expand Down