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
+67-23Lines changed: 67 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ It looks like this:
16
16
17
17
```js
18
18
functionshowMessage() {
19
-
alert( 'Hello everyone!' );
19
+
alert( 'Всем привет!' );
20
20
}
21
21
```
22
22
@@ -30,7 +30,7 @@ For instance:
30
30
31
31
```js run
32
32
functionshowMessage() {
33
-
alert( 'Hello everyone!' );
33
+
alert( 'Всем привет!' );
34
34
}
35
35
36
36
*!*
@@ -54,13 +54,17 @@ For example:
54
54
```js run
55
55
functionshowMessage() {
56
56
*!*
57
+
<<<<<<<HEAD
57
58
let message ="Hello, I'm JavaScript!"; // local variable
59
+
=======
60
+
let message ="Привет, я JavaScript!"; // локальная переменная
61
+
>>>>>>> translate examples
58
62
*/!*
59
63
60
64
alert( message );
61
65
}
62
66
63
-
showMessage(); //Hello, I'm JavaScript!
67
+
showMessage(); //Привет, я JavaScript!
64
68
65
69
alert( message ); // <-- Error! The variable is local to the function
66
70
```
@@ -70,57 +74,77 @@ alert( message ); // <-- Error! The variable is local to the function
70
74
A function can access an outer variable as well, for example:
71
75
72
76
```js run no-beautify
73
-
let*!*userName*/!*='John';
77
+
let*!*userName*/!*='Вася';
74
78
75
79
functionshowMessage() {
76
-
let message ='Hello, '+*!*userName*/!*;
80
+
let message ='Привет, '+*!*userName*/!*;
77
81
alert(message);
78
82
}
79
83
80
-
showMessage(); //Hello, John
84
+
showMessage(); //Привет, Вася
81
85
```
82
86
83
87
The function has full access to the outer variable. It can modify it as well.
84
88
85
89
For instance:
86
90
87
91
```js run
88
-
let*!*userName*/!*='John';
92
+
let*!*userName*/!*='Вася';
89
93
90
94
functionshowMessage() {
95
+
<<<<<<<HEAD
91
96
*!*userName*/!* = "Bob"; // (1) changed the outer variable
97
+
=======
98
+
*!*userName*/!* = "Петя"; // (1) изменяем значение внешней переменной
99
+
>>>>>>> translate examples
92
100
93
-
let message ='Hello, '+*!*userName*/!*;
101
+
let message ='Привет, '+*!*userName*/!*;
94
102
alert(message);
95
103
}
96
104
105
+
<<<<<<<HEAD
97
106
alert( userName ); // *!*John*/!* before the function call
98
107
99
108
showMessage();
100
109
101
110
alert( userName ); // *!*Bob*/!*, the value was modified by the function
111
+
=======
112
+
alert( userName ); // *!*Вася*/!* перед вызовом функции
113
+
114
+
showMessage();
115
+
116
+
alert( userName ); // *!*Петя*/!*, значение внешней переменной было изменено функцией
117
+
>>>>>>> translate examples
102
118
```
103
119
104
120
The outer variable is only used if there's no local one. So an occasional modification may happen if we forget `let`.
105
121
106
122
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:
107
123
108
124
```js run
109
-
let userName ='John';
125
+
let userName ='Вася';
110
126
111
127
functionshowMessage() {
112
128
*!*
129
+
<<<<<<<HEAD
113
130
let userName ="Bob"; // declare a local variable
131
+
=======
132
+
let userName ="Петя"; // объявляем локальную переменную
133
+
>>>>>>> translate examples
114
134
*/!*
115
135
116
-
let message ='Hello, '+ userName; // *!*Bob*/!*
136
+
let message ='Привет, '+ userName; // *!*Петя*/!*
117
137
alert(message);
118
138
}
119
139
120
140
// the function will create and use its own userName
121
141
showMessage();
122
142
143
+
<<<<<<<HEAD
123
144
alert( userName ); // *!*John*/!*, unchanged, the function did not access the outer variable
145
+
=======
146
+
alert( userName ); // *!*Вася*/!*, не изменилась, у функции нет доступа в внешней переменной
147
+
>>>>>>> translate examples
124
148
```
125
149
126
150
```smart header="Global variables"
@@ -143,8 +167,8 @@ function showMessage(*!*from, text*/!*) { // arguments: from, text
showMessage('Аня', "Как дела?"); //Аня: Как дела? (**)
148
172
*/!*
149
173
```
150
174
@@ -163,12 +187,17 @@ function showMessage(from, text) {
163
187
alert( from +': '+ text );
164
188
}
165
189
166
-
letfrom="Ann";
190
+
letfrom="Аня";
167
191
168
-
showMessage(from, "Hello"); // *Ann*: Hello
192
+
showMessage(from, "Привет"); // *Аня*: Привет
169
193
194
+
<<<<<<<HEAD
170
195
// the value of "from" is the same, the function modified a local copy
171
196
alert( from ); // Ann
197
+
=======
198
+
// значение "from" осталось прежним, функция изменила значение локальной переменной
199
+
alert( from ); // Аня
200
+
>>>>>>>translateexamples
172
201
```
173
202
174
203
## Default values
@@ -178,24 +207,34 @@ If a parameter is not provided, then its value becomes `undefined`.
178
207
For instance, the aforementioned function `showMessage(from, text)` can be called with a single argument:
179
208
180
209
```js
181
-
showMessage("Ann");
210
+
showMessage("Аня");
182
211
```
183
212
213
+
<<<<<<< HEAD
184
214
That's not an error. Such a call would output `"Ann: undefined"`. There's no `text`, so it's assumed that `text===undefined`.
215
+
=======
216
+
Это не приведёт к ошибке. Такой вызов выведет `"Аня: undefined"`. В вызове не указан параметр `text`, поэтому предполагается, что `text===undefined`.
217
+
>>>>>>> translate examples
185
218
186
219
If we want to use a "default" `text` in this case, then we can specify it after `=`:
187
220
188
221
```jsrun
189
-
functionshowMessage(from, *!*text="no text given"*/!*) {
222
+
functionshowMessage(from, *!*text="текст не добавлен"*/!*) {
190
223
alert( from +": "+ text );
191
224
}
192
225
193
-
showMessage("Ann"); //Ann: no text given
226
+
showMessage("Аня"); //Аня: текст не добавлен
194
227
```
195
228
229
+
<<<<<<< HEAD
196
230
Now if the `text` parameter is not passed, it will get the value `"no text given"`
197
231
198
232
Here `"no text given"` is a string, but it can be a more complex expression, which is only evaluated and assigned if the parameter is missing. So, this is also possible:
233
+
=======
234
+
Теперь, если параметр `text` не указан, его значением будет `"текст не добавлен"`
235
+
236
+
В данном случае `"текст не добавлен"` это строка, но на её месте могло бы быть и более сложное выражение, которое бы вычислялось и присваивалось при отсутствии параметра. Например:
0 commit comments