-
Notifications
You must be signed in to change notification settings - Fork 111
Functions - Local variables and outer variables headers #48
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
Merged
nazarepiedady
merged 4 commits into
javascript-tutorial:master
from
dheysonalves:master
Aug 13, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c55e153
Translate local variables and outer variables
dheysonalves 1a52ede
Merge branch 'master' of https://github.com/javascript-tutorial/pt.ja…
jonnathan-ls 1ce7d7c
refactor: fix suggested notes in code review
jonnathan-ls 506915a
feat: finalizes the translation of the article after updating with th…
jonnathan-ls File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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`. | ||
|
|
||
| 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: | ||
|
||
|
|
||
| ```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 | ||
|
||
| 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. | ||
|
||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
acontenceracontecer seesquercermosesquecermos do..."