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
@@ -137,12 +137,12 @@ alert( userName ); // 함수는 외부 변수에 접근하지 않습니다. 따
137
137
138
138
## 매개변수
139
139
140
-
매개변수(parameter)를 이용하면 임의의 데이터를 함수 안에 전달할 수 있습니다. 매개변수는 *인수(argument)* 라고 불리기도 합니다(매개변수와 인수는 엄밀히 같진 않지만, 튜토리얼 원문을 토대로 번역하였습니다 - 옮긴이). // [영어 원문 변경] We can pass arbitrary data to functions using parameters.
140
+
매개변수(parameter)를 이용하면 임의의 데이터를 함수 안에 전달할 수 있습니다. 매개변수는 *인자(parameter)* 라고 불리기도 합니다.
141
141
142
142
아래 예시에서 함수 showMessage는 매개변수 `from` 과 `text`를 가집니다.
143
143
144
144
```js run
145
-
functionshowMessage(*!*from, text*/!*) { //인수:from, text [영어 원문 변경:parameters:from, text]
145
+
functionshowMessage(*!*from, text*/!*) { //인자:from, text
When a value is passed as a function parameter, it'salsocalledan*argument*.
175
+
함수의 매개변수에 전달된 값을 *인수(argument)*라고 부르기도 합니다.
176
176
177
-
Inotherwords, toputthesetermsstraight:
177
+
더 정확한 이해를 돕기 위해 용어를 다시 한번 정리해볼까요?
178
178
179
-
-Aparameteristhevariablelistedinsidetheparenthesesinthefunctiondeclaration (it's a declaration time term).
180
-
- An argument is the value that is passed to the function when it is called (it'sacalltimeterm).
179
+
- 매개변수는 함수 선언 방식 괄호 사이에 있는 변수입니다(선언 시 쓰이는 용어).
180
+
- 인수는 함수를 호출할 때 매개변수에 전달되는 값입니다(호출 시 쓰이는 용어).
181
181
182
-
We declare functions listing their parameters, then call them passing arguments.
182
+
즉, 함수 선언 시 매개변수를 나열하게 되고, 함수를 호출할 땐 인수를 전달해 호출합니다.
183
183
184
-
In the example above, one might say: "the function `showMessage` is declared with two parameters, then called with two arguments: `from` and `"Hello"`".
184
+
위 예에서 함수 `showMessage`는 `from`과 `text`라는 두 매개변수를 사용해 선언되었고, 그 후 호출 시엔 `from`, `Hello`라는 두 인수를 사용해 호출되었습니다.
185
185
186
186
## 기본값
187
187
188
-
매개변수에 값을 전달하지 않으면 그 값은 `undefined`가 됩니다. [영어 원문 변경사항] If a function is called, but an argument is not provided, then the corresponding value becomes `undefined`.
188
+
함수 호출 시 매개변수에 인수를 전달하지 않으면 그 값은 `undefined`가 됩니다.
189
189
190
190
예시를 통해 이에 대해 알아봅시다. 위에서 정의한 함수 `showMessage(from, text)`는 매개변수가 2개지만, 아래와 같이 인수를 하나만 넣어서 호출할 수 있습니다.
191
191
192
192
```js
193
193
showMessage("Ann");
194
194
```
195
195
196
-
이렇게 코드를 작성해도 에러가 발생하지 않습니다. 두 번째 매개변수에 값을 전달하지 않았기 때문에 `text`엔 `undefiend`가 할당될 뿐입니다. 따라서 에러 없이 `"Ann: undefined"`가 출력됩니다.
196
+
이렇게 코드를 작성해도 에러가 발생하지 않습니다. 두 번째 매개변수에 값을 전달하지 않았기 때문에 `text`엔 `undefined`가 할당될 뿐입니다. 따라서 에러 없이 `"Ann: undefined"`가 출력됩니다.
197
197
198
-
매개변수에 값을 전달하지 않아도 그 값이 `undefined`가 되지 않게 하려면 '기본값(default value)'을 설정해주면 됩니다. 매개변수 오른쪽에 `=`을 붙이고 `undefined` 대신 설정하고자 하는 기본값을 써주면 되죠. [영문 변경] We can specify the so-called "default" (to use if omitted) value for a parameter in the function declaration, using `=`:
198
+
매개변수에 값을 전달하지 않아도 그 값이 `undefined`가 되지 않게 하려면 함수를 선언할 때 `=`를 사용해 '기본값(default value)'을 설정해주면 됩니다.
199
199
200
200
```js run
201
201
function showMessage(from, *!*text = "no text given"*/!*) {
@@ -205,10 +205,11 @@ function showMessage(from, *!*text = "no text given"*/!*) {
205
205
showMessage("Ann"); // Ann: no text given
206
206
```
207
207
208
-
이젠 `text`가 값을 전달받지 못해도 `undefined`대신 기본값 `"no text given"`이 할당됩니다.
208
+
이젠 `text`가 값을 전달받지 못해도 `undefined`대신 기본값 `"no text given"`이 할당됩니다.
209
209
210
-
The default value also jumps in if the parameter exists, but strictly equals `undefined`, like this:
210
+
매개변수에 값을 전달해도 그 값이 `undefined`와 엄격히 일치한다면 기본값이 할당됩니다.
211
211
212
+
예시:
212
213
```js
213
214
showMessage("Ann", undefined); // Ann: no text given
214
215
```
@@ -225,19 +226,20 @@ function showMessage(from, text = anotherFunction()) {
225
226
```smart header="매개변수 기본값 평가 시점"
226
227
자바스크립트에선 함수를 호출할 때마다 매개변수 기본값을 평가합니다. 물론 해당하는 매개변수가 없을 때만 기본값을 평가하죠.
227
228
228
-
위 예시에선 매개변수 `text`에 값이 없는 경우 `showMessage()`를 호출할 때마다 `anotherFunction()`이 호출됩니다. [영문 변경사항:] Intheexampleabove, `anotherFunction()`isn't called at all, if the `text` parameter is provided.
229
+
위 예시에선 매개변수 `text`에 값이 전달되는 경우 `anotherFunction()`은 호출되지 않습니다.
229
230
230
-
On the other hand, it'sindependentlycalledeverytimewhen`text`ismissing.
231
+
반면 `text`에 값이 없는 경우 `showMessage()`를 호출할 때마다 `anotherFunction()`이 호출됩니다.
231
232
232
233
```
233
234
234
-
````smart header="Default parameters in old JavaScript code"
235
-
Several years ago, JavaScript didn't support the syntax for default parameters. So people used other ways to specify them.
235
+
````smart header="구식 자바스크립트에서 매개변수 기본값 설정하는 방법"
236
+
몇 년 전만 해도 자바스크립트엔 매개변수 기본값 관련 구문이 없었습니다. 그래서 매개변수 기본값을 설정하려면 다른 방법을 사용해야만 했죠.
236
237
237
-
Nowadays, we can come across them in old scripts.
238
+
요즘에도 오래된 스크립트를 보다 보면 매개변수 기본값 설정 관련 코드를 접할 수 있습니다.
238
239
239
-
For example, an explicit check for `undefined`:
240
+
구식 코드에서는 매개변수 기본값 설정을 위해 먼저 매개변수 값이 `undefined`인지 명시적으로 확인하고, 일치하는 경우엔 기본값을 설정합니다.
240
241
242
+
예시:
241
243
```js
242
244
function showMessage(from, text) {
243
245
*!*
@@ -250,12 +252,12 @@ function showMessage(from, text) {
250
252
}
251
253
```
252
254
253
-
...Or using the `||` operator:
255
+
이 방법 말고도 논리 연산자 `||`를 사용해 매개변수 기본값을 설정하는 방법도 있습니다.
254
256
255
257
```js
256
258
function showMessage(from, text) {
257
-
//If the value of text is falsy, assign the default value
258
-
//this assumes that text == "" is the same as no text at all
259
+
// text의 값이 falsy면 기본값이 할당됨
260
+
// 이 방식은 text == ""일 경우, text에 값이 전달되지 않은것과 같다고 간주합니다..
259
261
text = text || 'notextgiven';
260
262
...
261
263
}
@@ -265,18 +267,17 @@ function showMessage(from, text) {
265
267
266
268
### 매개변수 기본값을 설정할 수 있는 또 다른 방법
267
269
268
-
<<<<<<< HEAD
269
-
가끔은 함수 선언부에서 매개변수 기본값을 설정하는 것 대신 함수가 실행되는 도중에 기본값을 설정하는 게 논리에 맞는 경우가 생기기도 합니다. [영문 변경] Sometimes it makes sense to assign default values for parameters at a later stage after the function declaration.
270
+
가끔은 함수를 선언할 때가 아닌 함수 선언 후에 매개변수 기본값을 설정하는 것이 적절한 경우도 있습니다.
270
271
271
-
이런 경우엔 일단 매개변수를 `undefined`와 비교하여 함수 호출 시 매개변수가 생략되었는지를 확인합니다. [영문 변경사항:] We can check if the parameter is passed during the function execution, by comparing it with `undefined`:
272
+
이런 경우엔 함수를 호출할 때 매개변수를 `undefined`와 비교하여 매개변수가 전달되었는지를 확인합니다.
272
273
273
274
```js run
274
275
function showMessage(text) {
275
276
// ...
276
277
277
278
*!*
278
-
if (text ===undefined) { //[영문 변경] if the parameter is missing
279
-
text ='빈 문자열';
279
+
if (text === undefined) { // 매개변수가 생략되었다면
280
+
text = '빈 문자열';
280
281
}
281
282
*/!*
282
283
@@ -289,7 +290,7 @@ showMessage(); // 빈 문자열
289
290
이렇게 `if`문을 쓰는 것 대신 논리 연산자 `||`를 사용할 수도 있습니다.
290
291
291
292
```js
292
-
// 매개변수가 생략되었거나 빈 문자열("")이 넘어오면 변수에 '빈 문자열'이 할당됩니다.
293
+
// 매개변수가 생략되었거나 빈 문자열("")이 넘어오면 변수에 '빈 문자열'이 할당됩니다.
293
294
function showMessage(text) {
294
295
text = text || '빈 문자열';
295
296
...
@@ -299,7 +300,7 @@ function showMessage(text) {
299
300
이 외에도 모던 자바스크립트 엔진이 지원하는 [nullish 병합 연산자(nullish coalescing operator)](info:nullish-coalescing-operator) `??`를 사용하면 `0`처럼 falsy로 평가되는 값들을 일반 값처럼 처리할 수 있어서 좋습니다.
300
301
301
302
```js run
302
-
// 매개변수 'count'가 넘어오지 않으면 'unknown'을 출력해주는 함수 [영문 변경] // if count is undefined or null, show "unknown"
303
+
// 매개변수 'count'가 `undefined` 또는 `null`이면 'unknown'을 출력해주는 함수
0 commit comments