-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-3.html
186 lines (175 loc) · 5.12 KB
/
11-3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="./public/favicon.ico" />
<meta http-equiv="cache-control" content="no-cache" />
<title></title>
<link rel="stylesheet" href=" https://necolas.github.io/normalize.css/8.0.1/normalize.css" />
<link rel="stylesheet" href="./hightlight/default.min.css" />
<link rel="stylesheet" href="./css/main.css" />
<link rel="stylesheet" href="./css/copybutton.css" />
<link rel="stylesheet" href="./css/hightlight.css" />
<script src="./hightlight/hightlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BEVZJDBC7Z"></script>
<script src="./js/gtag.js"></script>
</head>
<body>
<header>
<nav>
<h1>
<span id="toggle-menu"></span>
<a href="index.html"></a>
</h1>
</nav>
</header>
<main>
<aside>
<nav></nav>
</aside>
<article>
<h2 id="11-3">11-3 Trailing Commas</h2>
<p>
以往早期的 Javascript 版本在陣列和物件屬性最後加上逗號是會出錯。另外 function
放置參數的括號內也會出錯。
</p>
<h3>Array</h3>
<p>
版本控制上可以獲得好處。如下範例所示,在看差異時不會連同新增刪減的逗號也一併標記進去,增加可讀性。
</p>
<pre><code class="language-diff">
[
'foo',
+ 'baz',
'bar',
- 'baz',
]
</code></pre>
<p>結尾逗號不會影響陣列長度</p>
<pre><code class="language-js">
const arr = [
1,
2,
3,
];
arr; // [1, 2, 3]
arr.length; // 3
</code></pre>
<p>連續結尾逗號代表新增空白元素。</p>
<pre><code class="language-js">
const arr = [1, 2, 3, , ,];
arr.length; // 5
arr[3]; // undefined
</code></pre>
<h3>Objects</h3>
<p>物件屬性不可連續使用結尾逗號</p>
<pre><code class="language-js">
const object = {
foo: 'bar',
baz: 'qwerty',
age: 42,
};
const object2 = {
foo: 'bar',
baz: 'qwerty',
age: 42,,
};
// SyntaxError: Unexpected token ','
</code></pre>
<p>ES6 以後允許 function 使用結尾逗號。以下幾個版本的函式結果相等。</p>
<pre><code class="language-js">
function f(p) {}
function f(p,) {}
(p) => {};
(p,) => {};
</code></pre>
<p>其他案例: class 及 物件內 function</p>
<pre><code class="language-js">
class C {
one(a,) {}
two(a, b,) {}
}
const obj = {
one(a,) {},
two(a, b,) {},
};
</code></pre>
<h3>Function calls</h3>
<pre><code class="language-js">
f(p);
f(p,);
Math.max(10, 20);
Math.max(10, 20,);
</code></pre>
<h3>Illegal trailing commas</h3>
<pre><code class="language-js error-code">
function f(,) {} // SyntaxError: missing formal parameter
(,) => {}; // SyntaxError: expected expression, got ','
f(,) // SyntaxError: expected expression, got ','
function f(...p,) {} // SyntaxError: parameter after rest parameter
(...p,) => {} // SyntaxError: expected closing parenthesis, got ','
</code></pre>
<h3>Trailing commas in destructuring</h3>
<pre><code class="language-js">
// array destructuring with trailing comma
[a, b,] = [1, 2];
// object destructuring with trailing comma
const o = {
p: 42,
q: true,
};
const { p, q, } = o;
</code></pre>
<p>也無法用在展開運算子</p>
<pre><code class="language-js error-code">
const [a, ...b,] = [1, 2, 3];
// SyntaxError: rest element may not have a trailing comma
</code></pre>
<h3>Trailing commas in JSON</h3>
<p>JSON 不支援。</p>
<pre><code class="language-js error-code">
JSON.parse('[1, 2, 3, 4, ]');
JSON.parse('{"foo" : 1, }');
// SyntaxError JSON.parse: unexpected character
// at line 1 column 14 of the JSON data
</code></pre>
<pre><code class="language-js correct-code">
JSON.parse('[1, 2, 3, 4 ]');
JSON.parse('{"foo" : 1 }');
</code></pre>
<h3>Trailing commas in named imports and named exports</h3>
<h3>Named imports</h3>
<pre><code class="language-js">
import {
A,
B,
C,
} from 'D';
import { X, Y, Z, } from 'W';
import { A as B, C as D, E as F, } from 'Z';
</code></pre>
<h3>Named exports</h3>
<pre><code class="language-js ">
export {
A,
B,
C,
};
export { A, B, C, };
export { A as B, C as D, E as F, };
</code></pre>
<h3>Quantifier prefix</h3>
<pre><code class="language-js">
/x{2}/; // Exactly 2 occurrences of 'x'; equivalent to /xx/
/x{2,}/; // At least 2 occurrences of 'x'; equivalent to /xx+/
/x{2,4}/; // 2 to 4 occurrences of 'x'; equivalent to /xxx?x?/
</code></pre>
</article>
</main>
</body>
<script type="module" src="./js/main.js"></script>
</html>