You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/14-function-basics/article.md
+25-25Lines changed: 25 additions & 25 deletions
Display the source diff
Display the rich diff
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
47
47
48
48
## Variáveis locais
49
49
50
-
A variable declared inside a function is only visible inside that function.
50
+
Uma variável declarada dentro de uma função é apenas visível dentro dessa função.
51
51
52
-
For example:
52
+
Por exemplo:
53
53
54
54
```js run
55
55
functionshowMessage() {
56
56
*!*
57
-
let message ="Hello, I'm JavaScript!"; //local variable
57
+
let message ="Olá, Eu sou JavaScript!"; //variável local
58
58
*/!*
59
59
60
60
alert( message );
61
61
}
62
62
63
-
showMessage(); //Hello, I'm JavaScript!
63
+
showMessage(); //Olá, Eu sou JavaScript!
64
64
65
-
alert( message ); // <-- Error! The variable is local to the function
65
+
alert( message ); // <-- Erro! A variável é local para a função
66
66
```
67
67
68
-
## Outer variables
68
+
## Variáveis externas
69
69
70
-
A function can access an outer variable as well, for example:
70
+
Uma função também pode acessar uma variável externa, por exemplo:
71
71
72
72
```js run no-beautify
73
73
let*!*userName*/!*='John';
74
74
75
75
functionshowMessage() {
76
-
let message ='Hello, '+*!*userName*/!*;
76
+
let message ='Olá, '+*!*userName*/!*;
77
77
alert(message);
78
78
}
79
79
80
-
showMessage(); //Hello, John
80
+
showMessage(); //Olá, John
81
81
```
82
82
83
-
The function has full access to the outer variable. It can modify it as well.
83
+
A função tem acesso total à variável externa. Pode modificá-la também.
84
84
85
-
For instance:
85
+
Por exemplo:
86
86
87
87
```js run
88
88
let*!*userName*/!*='John';
89
89
90
90
functionshowMessage() {
91
-
*!*userName*/!* = "Bob"; // (1) changed the outer variable
91
+
*!*userName*/!* = "Bob"; // (1) mudou a variável externa
92
92
93
-
let message ='Hello, '+*!*userName*/!*;
93
+
let message ='Olá, '+*!*userName*/!*;
94
94
alert(message);
95
95
}
96
96
97
-
alert( userName ); // *!*John*/!* before the function call
97
+
alert( userName ); // *!*John*/!* antes da chamada da função
98
98
99
99
showMessage();
100
100
101
-
alert( userName ); // *!*Bob*/!*, the value was modified by the function
101
+
alert( userName ); // *!*Bob*/!*, o valor foi modificado pela função
102
102
```
103
103
104
-
The outer variable is only used if there's no local one. So an occasional modification may happen if we forget`let`.
104
+
A variável externa é apenas usada se não existir uma local. Então, uma modificação ocasional pode acontencer se esquercermos do`let`.
105
105
106
-
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:
106
+
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:
107
107
108
108
```js run
109
109
let userName ='John';
110
110
111
111
functionshowMessage() {
112
112
*!*
113
-
let userName ="Bob"; //declare a local variable
113
+
let userName ="Bob"; //declara uma variável local
114
114
*/!*
115
115
116
-
let message ='Hello, '+ userName; // *!*Bob*/!*
116
+
let message ='Olá, '+ userName; // *!*Bob*/!*
117
117
alert(message);
118
118
}
119
119
120
-
//the function will create and use its own userName
120
+
//a função criará e usará seu próprio userName
121
121
showMessage();
122
122
123
-
alert( userName ); // *!*John*/!*, unchanged, the function did not access the outer variable
123
+
alert( userName ); // *!*John*/!*, inalterado, a função não acessou a variável externa
124
124
```
125
125
126
-
```smart header="Global variables"
127
-
Variables declared outside of any function, such as the outer `userName` in the code above, are called *global*.
126
+
```smart header="Variáveis globais"
127
+
Variáveis declaradas fora de qualquer função, como o `userName` externo no código acima, são chamados de *globais*.
128
128
129
-
Global variables are visible from any function (unless shadowed by locals).
129
+
Variáveis globais são visíveis por qualquer função (a não ser que sejam sombreadas pelas locais).
130
130
131
-
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.
131
+
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.
0 commit comments