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
There's another very simple and concise syntax for creating functions, that's often better than Function Expressions.
3
+
함수 표현식보다 단순하고 간결한 문법으로 함수를 만들 수 있는 방법이 있습니다.
4
4
5
-
It's called "arrow functions", because it looks like this:
5
+
바로 화살표 함수(arrow function)를 사용하는 것입니다. 화살표 함수라는 이름은 문법의 생김새를 차용해 지어졌습니다.
6
6
7
7
```js
8
8
letfunc= (arg1, arg2, ...argN) => expression
9
9
```
10
10
11
-
...This creates a function `func` that accepts arguments `arg1..argN`, then evaluates the `expression` on the right side with their use and returns its result.
11
+
이렇게 코드를 작성하면 인자 `arg1..argN`를 받는 함수 `func`이 만들어집니다. 함수 `func`는 화살표(`=>`) 우측의 `표현식(expression)`을 평가하고, 평가 결과를 반환합니다.
12
12
13
-
In other words, it's the shorter version of:
13
+
아래 함수의 축약 버전이라고 할 수 있죠.
14
14
15
15
```js
16
16
letfunc=function(arg1, arg2, ...argN) {
17
17
return expression;
18
18
};
19
19
```
20
20
21
-
Let's see a concrete example:
21
+
좀 더 구체적인 예시를 살펴봅시다.
22
22
23
23
```js run
24
24
letsum= (a, b) => a + b;
25
25
26
-
/*This arrow function is a shorter form of:
26
+
/*위 화살표 함수는 아래 함수의 축약 버전입니다.
27
27
28
28
let sum = function(a, b) {
29
29
return a + b;
@@ -33,79 +33,79 @@ let sum = function(a, b) {
33
33
alert( sum(1, 2) ); // 3
34
34
```
35
35
36
-
As you can, see `(a, b) => a + b` means a function that accepts two arguments named `a` and `b`. Upon the execution, it evaluates the expression `a + b` and returns the result.
36
+
보시는 바와 같이 `(a, b) => a + b`는 인수 `a`와 `b`를 받는 함수입니다. `(a, b) => a + b`는 실행되는 순간 표현식 `a + b`를 평가하고 그 결과를 반환합니다.
37
37
38
-
-If we have only one argument, then parentheses around parameters can be omitted, making that even shorter.
38
+
-인수가 하나밖에 없다면 인수를 감싸는 괄호를 생략할 수 있습니다. 괄호를 생략하면 코드 길이를 더 줄일 수 있습니다.
39
39
40
-
For example:
40
+
예시:
41
41
42
42
```js run
43
43
*!*
44
44
letdouble=n=> n *2;
45
-
//roughly the same as: let double = function(n) { return n * 2 }
45
+
// let double = function(n) { return n * 2 }과 거의 동일합니다.
46
46
*/!*
47
47
48
48
alert( double(3) ); // 6
49
49
```
50
50
51
-
-If there are no arguments, parentheses will be empty (but they should be present):
51
+
-인수가 하나도 없을 땐 괄호를 비워놓으면 됩니다. 다만, 이 때 괄호는 생략할 수 없습니다.
52
52
53
53
```js run
54
-
let sayHi = () => alert("Hello!");
54
+
let sayHi = () => alert("안녕하세요!");
55
55
56
56
sayHi();
57
57
```
58
58
59
-
Arrow functions can be used in the same way as Function Expressions.
59
+
화살표 함수는 함수 표현식과 같은 방법으로 사용할 수 있습니다.
60
60
61
-
For instance, to dynamically create a function:
61
+
아래 예시와 같이 함수를 동적으로 만들 수 있습니다.
62
62
63
63
```js run
64
-
let age = prompt("What is your age?", 18);
64
+
let age = prompt("나이를 알려주세요.", 18);
65
65
66
66
let welcome = (age < 18) ?
67
-
() => alert('Hello') :
68
-
() => alert("Greetings!");
67
+
() => alert('안녕') :
68
+
() => alert("안녕하세요!");
69
69
70
-
welcome(); // ok now
70
+
welcome();
71
71
```
72
72
73
-
Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure.
73
+
화살표 함수를 처음 접하면 가독성이 떨어집니다. 익숙지 않기 때문입니다. 하지만 문법이 눈에 익기 시작하면 적응은 식은 죽 먹기가 됩니다.
74
74
75
-
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
75
+
함수 본문이 한 줄인 간단한 함수는 화살표 함수를 사용해서 만드는 게 편리합니다. 타이핑을 적게 해도 함수를 만들 수 있다는 장점이 있습니다.
76
76
77
-
## Multiline arrow functions
77
+
## 본문이 여러 줄인 화살표 함수
78
78
79
-
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
79
+
위에서 소개해 드린 화살표 함수들은 `=>` 왼쪽에 있는 인수를 이용해 `=>`오른쪽에 있는 표현식을 평가하는 함수들이었습니다.
80
80
81
-
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
81
+
그런데 평가해야 할 표현식이나 구문이 여러 개인 함수가 있을 수도 있습니다. 이 경우 역시 화살표 함수 문법을 사용해 함수를 만들 수 있습니다. 다만, 이때는 중괄호 안에 평가해야 할 코드를 넣어주어야 합니다. 그리고 `return`지시자를 사용해 명시적으로 결괏값을 반환해 주어야 합니다.
82
82
83
-
Like this:
83
+
아래와 같이 말이죠.
84
84
85
85
```js run
86
-
let sum = (a, b) => { // the curly brace opens a multiline function
86
+
let sum = (a, b) => { // 중괄호는 본문 여러 줄로 구성되어 있음을 알려줍니다.
87
87
let result = a + b;
88
88
*!*
89
-
return result; // if we use curly braces, then we need an explicit "return"
Here we praised arrow functions for brevity. But that's not all!
96
+
```smart header="아직 끝나지 않았습니다."
97
+
지금까진 간결함이라는 특징을 중심으로 화살표 함수에 대해 알아보았습니다. 하지만 이게 다가 아닙니다!
98
98
99
-
Arrow functions have other interesting features.
99
+
화살표 함수는 여기서 소개한 기능 이외에도 다른 흥미로운 기능을 지원합니다.
100
100
101
-
To study them in-depth, we first need to get to know some other aspects of JavaScript, so we'll return to arrow functions later in the chapter <info:arrow-functions>.
101
+
자세한 내용을 배우려면 자바스크립트의 다른 내용들을 더 알아야 합니다. 화살표 함수의 깊은 내용을 알기위해 필요한 내용을 배운 후에 <info:arrow-functions>에서 그 내용들을 다루도록 하겠습니다.
102
102
103
-
For now, we can already use arrow functions for one-line actions and callbacks.
103
+
지금까진 본문이 한 줄인 화살표 함수, 화살표 함수가 콜백으로 쓰인 경우에 대해서 알아보았습니다.
104
104
```
105
105
106
-
## Summary
106
+
## 요약
107
107
108
-
Arrow functions are handy for one-liners. They come in two flavors:
108
+
화살표 함수는 본문이 한 줄인 함수를 작성할 때 유용합니다. 본문이 한 줄이 아니라면 다른 방법으로 화살표 함수를 작성해야 합니다.
109
109
110
-
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
111
-
2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.
110
+
1.중괄호 없이 작성:`(...args) => expression`--화살표 오른쪽에 표현식을 둡니다. 함수는 이 표현식을 평가하고, 평가 결과를 반환합니다.
111
+
2.중괄호와 함께 작성:`(...args) => { body }`--본문이 여러 줄로 구성되었다면 중괄호를 사용해야 합니다. 다만, 이 경우는 반드시 `return`지시자를 사용해 반환 값을 명기해 주어야 합니다.
0 commit comments