|
1 | | -## Automatic Semicolon Insertion |
| 1 | +## 세미콜론을 자동으로 삽입해준다. |
2 | 2 |
|
3 | | -Although JavaScript has C style syntax, it does **not** enforce the use of |
4 | | -semicolons in the source code, so it is possible to omit them. |
| 3 | +JavaScript는 C와 문법이 비슷하지만, 꼭 코드에 semicolon을 사용하도록 강제하지 않는다. 그래서 생략할 수 있다. |
5 | 4 |
|
6 | | -JavaScript is not a semicolon-less language. In fact, it needs the |
7 | | -semicolons in order to understand the source code. Therefore, the JavaScript |
8 | | -parser **automatically** inserts them whenever it encounters a parse |
9 | | -error due to a missing semicolon. |
| 5 | +사실 JavaScript는 semicolon이 꼭 있어야 하고 없으면 이해하지 못한다. 그래서 JavaScript 파서는 semicolon이 없으면 **자동으로** semicolon을 추가한다. |
10 | 6 |
|
11 | 7 | var foo = function() { |
12 | | - } // parse error, semicolon expected |
| 8 | + } // 세미콜론이 없으니 에러 난다. |
13 | 9 | test() |
14 | 10 |
|
15 | | -Insertion happens, and the parser tries again. |
| 11 | +파서는 세미콜론을 삽입하고 다시 시도한다. |
16 | 12 |
|
17 | 13 | var foo = function() { |
18 | | - }; // no error, parser continues |
| 14 | + }; // 에러가 없어짐. |
19 | 15 | test() |
20 | 16 |
|
21 | | -The automatic insertion of semicolon is considered to be one of **biggest** |
22 | | -design flaws in the language because it *can* change the behavior of code. |
| 17 | +JavaScript에서 세미콜론을 자동으로 삽입한 것은 **대표적인** 설계 오류 중 하나다. 세미콜론 유무에 따라 *전혀* 다른 코드가 될 수 있다. |
23 | 18 |
|
24 | | -### How it Works |
| 19 | +### 어떻게 다를까? |
25 | 20 |
|
26 | | -The code below has no semicolons in it, so it is up to the parser to decide where |
27 | | -to insert them. |
| 21 | +코드에 세미콜론이 없으면 파서가 어디에 넣을지 결정한다. |
28 | 22 |
|
29 | 23 | (function(window, undefined) { |
30 | 24 | function test(options) { |
@@ -53,62 +47,53 @@ to insert them. |
53 | 47 |
|
54 | 48 | })(window) |
55 | 49 |
|
56 | | -Below is the result of the parser's "guessing" game. |
| 50 | +파서는 다음과 같이 삽입한다. |
57 | 51 |
|
58 | 52 | (function(window, undefined) { |
59 | 53 | function test(options) { |
60 | 54 |
|
61 | | - // Not inserted, lines got merged |
| 55 | + // 세미콜론을 넣는 것이 아니라 줄을 합친다. |
62 | 56 | log('testing!')(options.list || []).forEach(function(i) { |
63 | 57 |
|
64 | | - }); // <- inserted |
| 58 | + }); // <- 여기 |
65 | 59 |
|
66 | 60 | options.value.test( |
67 | 61 | 'long string to pass here', |
68 | 62 | 'and another long string to pass' |
69 | | - ); // <- inserted |
| 63 | + ); // <- 여기 |
70 | 64 |
|
71 | | - return; // <- inserted, breaks the return statement |
72 | | - { // treated as a block |
| 65 | + return; // <- 여기에 넣어서 그냥 반환시킨다. |
| 66 | + { // 파서는 단순 블럭이라고 생각하고 |
73 | 67 |
|
74 | | - // a label and a single expression statement |
| 68 | + // 단순한 레이블과 함수 |
75 | 69 | foo: function() {} |
76 | | - }; // <- inserted |
| 70 | + }; // <- 여기 |
77 | 71 | } |
78 | | - window.test = test; // <- inserted |
| 72 | + window.test = test; // <- 여기 |
79 | 73 |
|
80 | | - // The lines got merged again |
| 74 | + // 이 줄도 합쳐진다. |
81 | 75 | })(window)(function(window) { |
82 | | - window.someLibrary = {}; // <- inserted |
| 76 | + window.someLibrary = {}; // <- 여기 |
83 | 77 |
|
84 | | - })(window); //<- inserted |
| 78 | + })(window); //<- 여기에 파서는 세미콜론을 넣는다. |
85 | 79 |
|
86 | | -> **Note:** The JavaScript parser does not "correctly" handle return statements |
87 | | -> which are followed by a new line, while this is not neccessarily the fault of |
88 | | -> the automatic semicolon insertion, it can still be an unwanted side-effect. |
| 80 | +> **주의:** JavaScript 파서는 new line 문자가 뒤따라 오는 return 구문을 제대로 처리하지 못한다. 자동으로 세미콜론을 넣는 것 자체의 문제는 아니지만 어쨌든 여전히 문제로 남아있다. |
89 | 81 |
|
90 | | -The parser drastically changed the behavior of the code above. In certain cases, |
91 | | -it does the **wrong thing**. |
| 82 | +파서는 완전히 다른 코드로 만들어 버린다. 이것은 **오류**다. |
92 | 83 |
|
93 | | -### Leading Parenthesis |
| 84 | +### Parenthesis |
94 | 85 |
|
95 | | -In case of a leading parenthesis, the parser will **not** insert a semicolon. |
| 86 | +세미콜론 없이 괄호가 붙어 있으면 파서는 세미콜론을 넣지 않는다. |
96 | 87 |
|
97 | 88 | log('testing!') |
98 | 89 | (options.list || []).forEach(function(i) {}) |
99 | 90 |
|
100 | | -This code gets transformed into one line. |
| 91 | +파서는 다음과 같이 코드를 바꾼다. |
101 | 92 |
|
102 | 93 | log('testing!')(options.list || []).forEach(function(i) {}) |
103 | 94 |
|
104 | | -Chances are **very** high that `log` does **not** return a function; therefore, |
105 | | -the above will yield a `TypeError` stating that `undefined is not a function`. |
| 95 | +`log` 함수가 함수를 반환할 가능성은 거의 없다. 아마도 `undefined is not a function`이라는 `TypeError`가 발생할 거다. |
106 | 96 |
|
107 | | -### In Conclusion |
108 | | - |
109 | | -It is highly recommended to **never** omit semicolons; it is also advocated to |
110 | | -keep braces on the same line with their corresponding statements and to never omit |
111 | | -them for one single-line `if` / `else` statements. Both of these measures will |
112 | | -not only improve the consistency of the code, but they will also prevent the |
113 | | -JavaScript parser from changing its behavior. |
| 97 | +### 결론 |
114 | 98 |
|
| 99 | +세미콜론은 반드시 사용해야 한다. 그리고 `{}`도 생략하지 않고 꼭 사용하는 것이 좋다. 한 줄밖에 안 되는 `if` / `else` 블럭에서도 꼭 사용해야 한다. 이 두 가지 규칙을 잘 지키면 JavaScript 파서가 잘못 해석하는 일을 미리 방지하고 코드도 튼튼해진다. |
0 commit comments