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/15-function-basics/article.md
+33-19Lines changed: 33 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ function showMessage() {
23
23
`function` 키워드, *함수 이름*, 괄호로 둘러싼 매개변수를 차례로 써주면 함수를 선언할 수 있습니다. 위 함수에는 매개변수가 없는데, 만약 매개변수가 여러 개 있다면 각 매개변수를 콤마로 구분해 줍니다. 이어서 함수를 구성하는 코드의 모임인 '함수 본문(body)'을 중괄호로 감싸 붙여줍시다.
@@ -137,26 +137,23 @@ alert( userName ); // 함수는 외부 변수에 접근하지 않습니다. 따
137
137
138
138
## 매개변수
139
139
140
-
매개변수(parameter)를 이용하면 임의의 데이터를 함수 안에 전달할 수 있습니다. 매개변수는 *인수(argument)* 라고 불리기도 합니다(매개변수와 인수는 엄밀히 같진 않지만, 튜토리얼 원문을 토대로 번역하였습니다 - 옮긴이).
140
+
매개변수(parameter)를 이용하면 임의의 데이터를 함수 안에 전달할 수 있습니다. 매개변수는 *인수(argument)* 라고 불리기도 합니다(매개변수와 인수는 엄밀히 같진 않지만, 튜토리얼 원문을 토대로 번역하였습니다 - 옮긴이). // [영어 원문 변경] We can pass arbitrary data to functions using parameters.
141
141
142
142
아래 예시에서 함수 showMessage는 매개변수 `from` 과 `text`를 가집니다.
143
143
144
144
```js run
145
-
functionshowMessage(*!*from, text*/!*) { // 인수:from, text
145
+
functionshowMessage(*!*from, text*/!*) { // 인수:from, text [영어 원문 변경:parameters:from, text]
When a value is passed as a function parameter, it'salsocalledan*argument*.
176
+
177
+
Inotherwords, toputthesetermsstraight:
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).
181
+
182
+
We declare functions listing their parameters, then call them passing arguments.
183
+
184
+
In the example above, one might say: "the function `sayMessage` is declared with two parameters, then called with two arguments: `from` and `"Hello"`".
185
+
178
186
## 기본값
179
187
180
-
매개변수에 값을 전달하지 않으면 그 값은 `undefined`가 됩니다.
188
+
매개변수에 값을 전달하지 않으면 그 값은 `undefined`가 됩니다. [영어 원문 변경사항] If a function is called, but an argument is not provided, then the corresponding value becomes `undefined`.
181
189
182
190
예시를 통해 이에 대해 알아봅시다. 위에서 정의한 함수 `showMessage(from, text)`는 매개변수가 2개지만, 아래와 같이 인수를 하나만 넣어서 호출할 수 있습니다.
183
191
184
192
```js
185
193
showMessage("Ann");
186
194
```
187
195
188
-
이렇게 코드를 작성해도 에러가 발생하지 않습니다. 두 번째 매개변수에 값을 전달하지 않았기 때문에 `text`엔 `undefiend`가 할당될 뿐입니다. 따라서 에러 없이 `"Ann: undefined"`가 출력됩니다.
196
+
이렇게 코드를 작성해도 에러가 발생하지 않습니다. 두 번째 매개변수에 값을 전달하지 않았기 때문에 `text`엔 `undefiend`가 할당될 뿐입니다. 따라서 에러 없이 `"Ann: undefined"`가 출력됩니다.
189
197
190
-
매개변수에 값을 전달하지 않아도 그 값이 `undefined`가 되지 않게 하려면 '기본값(default value)'을 설정해주면 됩니다. 매개변수 오른쪽에 `=`을 붙이고 `undefined` 대신 설정하고자 하는 기본값을 써주면 되죠.
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 `=`:
191
199
192
200
```js run
193
201
function showMessage(from, *!*text = "no text given"*/!*) {
@@ -211,20 +219,25 @@ function showMessage(from, text = anotherFunction()) {
211
219
```smartheader="매개변수 기본값 평가 시점"
212
220
자바스크립트에선 함수를 호출할 때마다 매개변수 기본값을 평가합니다. 물론 해당하는 매개변수가 없을 때만 기본값을 평가하죠.
213
221
214
-
위 예시에선 매개변수 `text`에 값이 없는 경우 `showMessage()`를 호출할 때마다 `anotherFunction()`이 호출됩니다.
222
+
위 예시에선 매개변수 `text`에 값이 없는 경우 `showMessage()`를 호출할 때마다 `anotherFunction()`이 호출됩니다. [영문 변경사항:] Intheexampleabove, `anotherFunction()`isn't called at all, if the `text` parameter is provided.
223
+
224
+
On the other hand, it'sindependentlycalledeverytimewhen`text`ismissing.
225
+
215
226
```
216
227
217
228
### 매개변수 기본값을 설정할 수 있는 또 다른 방법
218
229
219
-
가끔은 함수 선언부에서 매개변수 기본값을 설정하는 것 대신 함수가 실행되는 도중에 기본값을 설정하는 게 논리에 맞는 경우가 생기기도 합니다.
230
+
가끔은 함수 선언부에서 매개변수 기본값을 설정하는 것 대신 함수가 실행되는 도중에 기본값을 설정하는 게 논리에 맞는 경우가 생기기도 합니다. [영문 변경] Sometimes it makes sense to assign default values for parameters not in the function declaration, but at a later stage.
220
231
221
-
이런 경우엔 일단 매개변수를 `undefined`와 비교하여 함수 호출 시 매개변수가 생략되었는지를 확인합니다.
232
+
이런 경우엔 일단 매개변수를 `undefined`와 비교하여 함수 호출 시 매개변수가 생략되었는지를 확인합니다. [영문 변경사항:] We can check if the parameter is passed during the function execution, by comparing it with `undefined`:
222
233
223
234
```jsrun
224
235
functionshowMessage(text) {
236
+
// ...
237
+
225
238
*!*
226
-
if (text ===undefined) {
227
-
text ='빈 문자열';
239
+
if (text ===undefined) {// [영문 변경] if the parameter is missing
240
+
text ='빈 문자열';
228
241
}
229
242
*/!*
230
243
@@ -234,10 +247,11 @@ function showMessage(text) {
234
247
showMessage(); // 빈 문자열
235
248
```
236
249
237
-
이렇게 `if`문을 쓰는 것 대신 논리 연산자 `||`를 사용할 수도 있습니다.
250
+
<<<<<<< HEAD
251
+
이렇게 `if`문을 쓰는 것 대신 논리 연산자 `||`를 사용할 수도 있습니다. [영문 번역] ...Or we could use the `??` operator:
238
252
239
253
```js
240
-
// 매개변수가 생략되었거나 빈 문자열("")이 넘어오면 변수에 '빈 문자열'이 할당됩니다.
254
+
// 매개변수가 생략되었거나 빈 문자열("")이 넘어오면 변수에 '빈 문자열'이 할당됩니다.
241
255
functionshowMessage(text) {
242
256
text = text ||'빈 문자열';
243
257
...
@@ -247,7 +261,7 @@ function showMessage(text) {
247
261
이 외에도 모던 자바스크립트 엔진이 지원하는 [nullish 병합 연산자(nullish coalescing operator)](info:nullish-coalescing-operator) `??`를 사용하면 `0`처럼 falsy로 평가되는 값들을 일반 값처럼 처리할 수 있어서 좋습니다.
248
262
249
263
```js run
250
-
// 매개변수 'count'가 넘어오지 않으면 'unknown'을 출력해주는 함수
264
+
// 매개변수 'count'가 넘어오지 않으면 'unknown'을 출력해주는 함수 [영문 변경] // if count is undefined or null, show "unknown"
0 commit comments