|
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, it is possible to omit them. |
| 3 | +尽管 JavaScript 有 C 的代码风格,但是它**不**强制要求在代码中使用分号,实际上可以省略它们。 |
5 | 4 |
|
6 |
| -But JavaScript is not a semicolon-less language, it in fact 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 不是一个没有分号的语言,恰恰相反上它需要分号来就解析源代码。 |
| 6 | +因此 JavaScript 解析器在遇到由于缺少分号导致的解析错误时,会**自动**在源代码中插入分号。 |
10 | 7 |
|
11 | 8 | var foo = function() {
|
12 |
| - } // parse error, semicolon expected |
| 9 | + } // 解析错误,分号丢失 |
13 | 10 | test()
|
14 | 11 |
|
15 |
| -Insertion happens, and the parser tries again. |
| 12 | +自动插入分号,解析器重新解析。 |
16 | 13 |
|
17 | 14 | var foo = function() {
|
18 |
| - }; // no error, parser continues |
| 15 | + }; // 没有错误,解析继续 |
19 | 16 | test()
|
20 | 17 |
|
21 |
| -The automatic insertion of semicolon is considered to be one of **biggest** |
22 |
| -design flaws in the language, as it *can* change the behavior of code. |
| 18 | +自动的分号插入被认为是 JavaScript 语言**最大**的设计缺陷之一,因为它*能*改变代码的行为。 |
23 | 19 |
|
24 |
| -### How it works |
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 | +### 工作原理(How it works) |
| 22 | + |
| 23 | +下面的代码没有分号,因此解析器需要自己判断需要在哪些地方插入分号。 |
| 24 | + |
28 | 25 |
|
29 | 26 | (function(window, undefined) {
|
30 | 27 | function test(options) {
|
@@ -53,62 +50,60 @@ to insert them.
|
53 | 50 |
|
54 | 51 | })(window)
|
55 | 52 |
|
56 |
| -Below is the result of the parser's "guessing" game. |
| 53 | +下面是解析器"猜测"的结果。 |
57 | 54 |
|
58 | 55 | (function(window, undefined) {
|
59 | 56 | function test(options) {
|
60 | 57 |
|
61 | 58 | // Not inserted, lines got merged
|
62 | 59 | log('testing!')(options.list || []).forEach(function(i) {
|
63 | 60 |
|
64 |
| - }); // <- inserted |
| 61 | + }); // <- 插入分号 |
65 | 62 |
|
66 | 63 | options.value.test(
|
67 | 64 | 'long string to pass here',
|
68 | 65 | 'and another long string to pass'
|
69 |
| - ); // <- inserted |
| 66 | + ); // <- 插入分号 |
70 | 67 |
|
71 |
| - return; // <- inserted, breaks the return statement |
72 |
| - { // treated as a block |
| 68 | + return; // <- 插入分号, 改变了 return 表达式的行为 |
| 69 | + { // 作为一个代码段处理 |
73 | 70 |
|
74 | 71 | // a label and a single expression statement
|
75 | 72 | foo: function() {}
|
76 |
| - }; // <- inserted |
| 73 | + }; // <- 插入分号 |
77 | 74 | }
|
78 |
| - window.test = test; // <- inserted |
| 75 | + window.test = test; // <- 插入分号 |
79 | 76 |
|
80 | 77 | // The lines got merged again
|
81 | 78 | })(window)(function(window) {
|
82 |
| - window.someLibrary = {}; // <- inserted |
| 79 | + window.someLibrary = {}; // <- 插入分号 |
| 80 | + |
| 81 | + })(window); //<- 插入分号 |
83 | 82 |
|
84 |
| - })(window); //<- inserted |
| 83 | +> **注意:** JavaScript 不能正确的处理 return 表达式紧跟换行符的情况, |
| 84 | +> 虽然这不能算是自动分号插入的错误,但这确实是一种不希望的副作用。 |
85 | 85 |
|
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. |
89 | 86 |
|
90 |
| -The parser drastically changed the behavior of the code above, in certain cases |
91 |
| -it does the **wrong thing**. |
| 87 | +解析器显著改变了上面代码的行为,在另外一些情况下也会做出**错误的处理**。 |
92 | 88 |
|
93 |
| -### Leading parenthesis |
94 | 89 |
|
95 |
| -In case of a leading parenthesis, the parser will **not** insert a semicolon. |
| 90 | +### 前置括号(Leading parenthesis) |
| 91 | + |
| 92 | +在前置括号的情况下,解析器**不会**自动插入分号。 |
96 | 93 |
|
97 | 94 | log('testing!')
|
98 | 95 | (options.list || []).forEach(function(i) {})
|
99 | 96 |
|
100 |
| -This code gets transformed into one line. |
| 97 | +上面代码被解析器转换为一行。 |
101 | 98 |
|
102 | 99 | log('testing!')(options.list || []).forEach(function(i) {})
|
103 | 100 |
|
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`. |
| 101 | +`log` 函数的执行结果**极大**可能**不是**函数;这种情况下就会出现 `TypeError` 的错误,详细错误信息可能是 `undefined is not a function`。 |
| 102 | + |
106 | 103 |
|
107 |
| -### In conclusion |
| 104 | +### 结论(In conclusion) |
108 | 105 |
|
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, they will also prevent the |
113 |
| -JavaScript parser from changing its behavior. |
| 106 | +建议**绝对**不要省略分号,同时也提倡将花括号和相应的表达式放在一行, |
| 107 | +对于只有一行代码的 `if` 或者 `else` 表达式,也不应该省略花括号。 |
| 108 | +这些良好的编程习惯不仅可以提到代码的一致性,而且可以防止解析器改变代码行为的错误处理。 |
114 | 109 |
|
0 commit comments