-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaScript 以及 ECMAScript 6.html
527 lines (527 loc) · 123 KB
/
JavaScript 以及 ECMAScript 6.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- common.css -->
<style>* {-webkit-tap-highlight-color: rgba(0,0,0,0);}html {-webkit-text-size-adjust: none;}body {font-family: Arial, Helvetica, sans-serif;margin: 0;color: #333;word-wrap: break-word;}h1, h2, h3, h4, h5, h6 {line-height: 1.1;}img {max-width: 100% !important;}blockquote {margin: 0;padding: 0 15px;color: #777;border-left: 4px solid #ddd;}hr {background-color: #ddd;border: 0;height: 1px;margin: 15px 0;}code {font-family: Menlo, Consolas, 'Ubuntu Mono', Monaco, 'source-code-pro', monospace;line-height: 1.4;margin: 0;padding: 0.2em 0;font-size: 85%;background-color: rgba(0,0,0,0.04);border-radius: 3px;}pre > code {margin: 0;padding: 0;font-size: 100%;word-break: normal;background: transparent;border: 0;}ol {list-style-type: decimal;}ol ol, ul ol {list-style-type: lower-latin;}ol ol ol, ul ol ol, ul ul ol, ol ul ol {list-style-type: lower-roman;}table {border-spacing: 0;border-collapse: collapse;margin-top: 0;margin-bottom: 16px;}table th {font-weight: bold;}table th, table td {padding: 6px 13px;border: 1px solid #ddd;}table tr {border-top: 1px solid #ccc;}table tr:nth-child(even) {background-color: #f8f8f8;}input[type="checkbox"] {cursor: default;margin-right: 0.5em;font-size: 13px;}.task-list-item {list-style-type: none;}.task-list-item+.task-list-item {margin-top: 3px;}.task-list-item input {float: left;margin: 0.3em 1em 0.25em -1.6em;vertical-align: middle;}#tag-field {margin: 8px 2px 10px;}#tag-field .tag {display: inline-block;background: #cadff3;border-radius: 4px;padding: 1px 8px;color: black;font-size: 12px;margin-right: 10px;line-height: 1.4;}</style>
<!-- ace-static.css -->
<style>.ace_static_highlight {white-space: pre-wrap;}.ace_static_highlight .ace_gutter {width: 2em;text-align: right;padding: 0 3px 0 0;margin-right: 3px;}.ace_static_highlight.ace_show_gutter .ace_line {padding-left: 2.6em;}.ace_static_highlight .ace_line {position: relative;}.ace_static_highlight .ace_gutter-cell {-moz-user-select: -moz-none;-khtml-user-select: none;-webkit-user-select: none;user-select: none;top: 0;bottom: 0;left: 0;position: absolute;}.ace_static_highlight .ace_gutter-cell:before {content: counter(ace_line, decimal);counter-increment: ace_line;}.ace_static_highlight {counter-reset: ace_line;}</style>
<style>.ace-xcode .ace_gutter {background: #e8e8e8;color: #333}.ace-xcode .ace_print-margin {width: 1px;background: #e8e8e8}.ace-xcode {background-color: #FFFFFF;color: #000000}.ace-xcode .ace_cursor {color: #000000}.ace-xcode .ace_marker-layer .ace_selection {background: #B5D5FF}.ace-xcode.ace_multiselect .ace_selection.ace_start {box-shadow: 0 0 3px 0px #FFFFFF;border-radius: 2px}.ace-xcode .ace_marker-layer .ace_step {background: rgb(198, 219, 174)}.ace-xcode .ace_marker-layer .ace_bracket {margin: -1px 0 0 -1px;border: 1px solid #BFBFBF}.ace-xcode .ace_marker-layer .ace_active-line {background: rgba(0, 0, 0, 0.071)}.ace-xcode .ace_gutter-active-line {background-color: rgba(0, 0, 0, 0.071)}.ace-xcode .ace_marker-layer .ace_selected-word {border: 1px solid #B5D5FF}.ace-xcode .ace_constant.ace_language,.ace-xcode .ace_keyword,.ace-xcode .ace_meta,.ace-xcode .ace_variable.ace_language {color: #C800A4}.ace-xcode .ace_invisible {color: #BFBFBF}.ace-xcode .ace_constant.ace_character,.ace-xcode .ace_constant.ace_other {color: #275A5E}.ace-xcode .ace_constant.ace_numeric {color: #3A00DC}.ace-xcode .ace_entity.ace_other.ace_attribute-name,.ace-xcode .ace_support.ace_constant,.ace-xcode .ace_support.ace_function {color: #450084}.ace-xcode .ace_fold {background-color: #C800A4;border-color: #000000}.ace-xcode .ace_entity.ace_name.ace_tag,.ace-xcode .ace_support.ace_class,.ace-xcode .ace_support.ace_type {color: #790EAD}.ace-xcode .ace_storage {color: #C900A4}.ace-xcode .ace_string {color: #DF0002}.ace-xcode .ace_comment {color: #008E00}.ace-xcode .ace_indent-guide {background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAACCAYAAACZgbYnAAAAE0lEQVQImWP4////f4bLly//BwAmVgd1/w11/gAAAABJRU5ErkJggg==) right repeat-y}</style>
<!-- export.css -->
<style>
body{margin:0 auto;max-width:800px;line-height:1.4}
#nav{margin:5px 0 10px;font-size:15px}
#titlearea{border-bottom:1px solid #ccc;font-size:17px;padding:10px 0;}
#contentarea{font-size:15px;margin:16px 0}
.cell{outline:0;min-height:20px;margin:5px 0;padding:5px 0;}
.code-cell{font-family:Menlo,Consolas,'Ubuntu Mono',Monaco,'source-code-pro',monospace;font-size:12px;}
.latex-cell{white-space:pre-wrap;}
</style>
<!-- User CSS -->
<style> .text-cell {font-size: 15px;}.code-cell {font-size: 12px;}.markdown-cell {font-size: 15px;}.latex-cell {font-size: 15px;}</style>
</head>
<body>
<div id="nav"><div>Next: <a href='Node.js.html'>Node.js</a>, Previous: <a href='技術簡介.html'>技術簡介</a>, Up: <a href='index.html'>Index</a></div></div>
<div id="titlearea">
<h2>JavaScript 以及 ECMAScript 6</h2>
</div>
<div id="contentarea"><div class="cell markdown-cell"><p>JavaScript 基本語法(ECMAScript 5 部分,與大部分瀏覽器相容)</p>
</div><div class="cell markdown-cell"><h2 id="-">變數、型態</h2>
<p>首先,JavaScript 的變數無法定義型態,在第一次給值的時候,會自動產生型態。</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// Number <span class="ace_cjk" style="width:20px">型</span><span class="ace_cjk" style="width:20px">態</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">someNumber</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">123</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">someNumber</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">12.23</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// String <span class="ace_cjk" style="width:20px">型</span><span class="ace_cjk" style="width:20px">態</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">someString</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string">"string"</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">someString</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string">'someString'</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// undefined <span class="ace_cjk" style="width:20px">值</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">someUndefined</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// <span class="ace_cjk" style="width:20px">陣</span><span class="ace_cjk" style="width:20px">列</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">array</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">3</span><span class="ace_paren ace_rparen">]</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// <span class="ace_cjk" style="width:20px">字</span><span class="ace_cjk" style="width:20px">典</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">dict</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">a</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">b</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_constant ace_numeric">2</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><p>用 <code>var</code> 定義出來的變數若不是全域變數則為函數變數,是跟著函數跑的,而不是跟著大括號。</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">global_variable</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">123</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">function</span> <span class="ace_entity ace_name ace_function">f</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">var</span> <span class="ace_identifier">t</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">1</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">while</span> <span class="ace_paren ace_lparen">(</span><span class="ace_identifier">t</span><span class="ace_keyword ace_operator">--</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">var</span> <span class="ace_identifier">x</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">4</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_comment">// <span class="ace_cjk" style="width:20px">在</span><span class="ace_cjk" style="width:20px">這</span><span class="ace_cjk" style="width:20px">裡</span><span class="ace_cjk" style="width:20px">可</span><span class="ace_cjk" style="width:20px">以</span><span class="ace_cjk" style="width:20px">用</span> x</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// <span class="ace_cjk" style="width:20px">不</span><span class="ace_cjk" style="width:20px">能</span><span class="ace_cjk" style="width:20px">使</span><span class="ace_cjk" style="width:20px">用</span> x <span class="ace_cjk" style="width:20px">了</span></span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-">運算子</h2>
<p>運算方面包含最基本的 <code>+</code>、<code>-</code>、<code>*</code>、<code>/</code>、<code>%</code>、<code>++</code>、<code>--</code>。</p>
<p>指定方面包含最基本的 <code>=</code>、<code>+=</code>、<code>-=</code>、<code>*=</code>、<code>/=</code>、<code>%=</code>。</p>
<p>比較方面包含 <code>==</code>、<code>===</code>、<code>!=</code>、<code>!==</code>、<code>></code>、<code><</code>、<code>>=</code>、<code><=</code> 以及 <code>? :</code>。</p>
<p>型態方面包含 <code>typeof</code> 以及 <code>instanceof</code>。</p>
<p>其中,若用 <code>==</code> 比較,則 <code>1 == "1"</code>,若要保證型態相同,則應使用 <code>===</code>。</p>
<p><code>typeof</code> 以字串回傳型態,<code>instanceof</code> 檢查物件是否為類別的實例。</p>
</div><div class="cell markdown-cell"><h2 id="-">函數</h2>
<p>JavaScript 的函數非常簡單</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">function</span> <span class="ace_entity ace_name ace_function">add</span><span class="ace_paren ace_lparen">(</span><span class="ace_variable ace_parameter">a</span><span class="ace_punctuation ace_operator">, </span><span class="ace_variable ace_parameter">b</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_identifier">a</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_identifier">b</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><p>函數本身其實也是物件</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_entity ace_name ace_function">f</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_variable ace_parameter">a</span><span class="ace_punctuation ace_operator">, </span><span class="ace_variable ace_parameter">b</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_identifier">a</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_identifier">b</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">argumentCount</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">f</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_constant">length</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">x</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">f</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_paren ace_rparen">)</span>
</div></div></div></div><div class="cell markdown-cell"><p>函數的 <code>bind(thisArg, arg1, arg2, ...)</code> 可以用來製造新的函數</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_constant">x</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">1</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">obj</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">x</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_entity ace_name ace_function">t</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_storage ace_type">function</span> <span class="ace_paren ace_lparen">(</span><span class="ace_variable ace_parameter">y</span><span class="ace_punctuation ace_operator"> </span><span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">0</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_constant">x</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_identifier">y</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">obj</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">t</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_comment">// 2</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">f</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">obj</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">t</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">f</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_comment">// 1</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">f2</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">f</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">bind</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">obj</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">f2</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_comment">// 2</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_entity ace_name ace_function">f3</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_variable ace_parameter">a</span><span class="ace_punctuation ace_operator">, </span><span class="ace_variable ace_parameter">b</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_identifier">a</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_identifier">b</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">f4</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">f3</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">bind</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_language">undefined</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">4</span><span class="ace_paren ace_rparen">)</span> <span class="ace_comment">// f4 = function(b) {return b + 4}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">f4</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">2</span><span class="ace_paren ace_rparen">)</span> <span class="ace_comment">// 6</span>
</div></div></div></div><div class="cell markdown-cell"><p>JavaScript 函數所意指的 <code>this</code> 跟在哪裡呼叫那個函數有關係,並不是跟著函數被定義的位置跑。可以用 <code>bind()</code> 指定這個函數的 <code>this</code> 指的是哪個物件。</p>
<h3 id="-">回傳函數</h3>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">function</span> <span class="ace_entity ace_name ace_function">factory</span><span class="ace_paren ace_lparen">(</span><span class="ace_variable ace_parameter">a</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">b</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_identifier">a</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_identifier">b</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">t</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">factory</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">4</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">t</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">5</span><span class="ace_paren ace_rparen">)</span> <span class="ace_comment">// 9</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-">物件</h2>
<p>JavaScript 中的物件非常簡單,就是一個字典</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">car</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">manufacturer</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_string">'Volvo'</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">model</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_string">'V40'</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_entity ace_name ace_function">name</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">manufacturer</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_string">" "</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">model</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">car_name</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">car</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">name</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span>
</div></div></div></div><div class="cell markdown-cell"><p>JavaScript 的類別是以原形為主,物件可以不用為類別的實例,但是也可以從類別來建立物件</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_entity ace_name ace_function">Car</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_variable ace_parameter">manufacturer</span><span class="ace_punctuation ace_operator">, </span><span class="ace_variable ace_parameter">model</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">manufacturer</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">manufacturer</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">model</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">model</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_entity ace_name ace_function">name</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">manufacturer</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_string">" "</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">model</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">car</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_keyword">new</span> <span class="ace_identifier">Car</span><span class="ace_paren ace_lparen">(</span><span class="ace_string">'Volvo'</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_string">'XC60'</span><span class="ace_paren ace_rparen">)</span>
</div></div></div></div><div class="cell markdown-cell"><p>也可以直接修改原形</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">Car</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function">prototype</span><span class="ace_punctuation ace_operator">.</span><span class="ace_entity ace_name ace_function">name</span> <span class="ace_keyword ace_operator">=</span><span class="ace_punctuation ace_operator"> </span><span class="ace_variable ace_parameter">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">model</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-">字串的方法以及屬性</h2>
<p>確認內容</p>
<ul>
<li><code>includes(target)</code></li><li><code>startsWith(target)</code></li><li><code>endsWith(target)</code></li></ul>
<p>在字串中搜尋</p>
<ul>
<li><code>indexOf(target, indexBegin = 0)</code></li><li><code>lastIndexOf(target, indexBegin = 0)</code></li><li><code>search(target, indexBegin = 0)</code></li><li><code>search(pattern, indexBegin = 0)</code></li></ul>
<p>擷取部分內容</p>
<ul>
<li><code>slice(begin, end)</code></li><li><code>substring(begin, end)</code></li><li><code>substr(begin, length)</code></li><li><code>trim()</code></li></ul>
<p><code>slice</code> 的 <code>begin</code> 以及 <code>end</code> 若為負數,則會用倒數的方式。<br><code>trim</code> 會刪除開頭結尾的空白字元。</p>
<p>取代內容</p>
<ul>
<li><code>replace(target, content)</code></li><li><code>replace(pattern, content)</code></li></ul>
<p>轉換大小寫</p>
<ul>
<li><code>toUpperCase()</code></li><li><code>toLowerCase()</code></li></ul>
<p>取得字元</p>
<ul>
<li><code>charAt(index)</code></li><li><code>charCodeAt(index)</code></li></ul>
<p>轉換成陣列</p>
<ul>
<li><code>split(splitPoint)</code></li></ul>
<p>若用 <code>split('')</code> 則會轉換為字元陣列。</p>
</div><div class="cell markdown-cell"><h2 id="-regular-expression-">正規表示式(Regular Expression)</h2>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">expression</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string ace_regexp">/exp/</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">expression</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string ace_regexp">/exp/igm</span>
</div></div></div></div><div class="cell markdown-cell"><p>第一個部分是樣式本身,第二個部分是修飾子。</p>
<table>
<thead>
<tr>
<th>修飾子</th>
<th>意義</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>i</code></td>
<td>不論大小寫。</td>
</tr>
<tr>
<td><code>g</code></td>
<td>搜尋整個字串。</td>
</tr>
<tr>
<td><code>m</code></td>
<td>進行跨行搜尋。</td>
</tr>
<tr>
<td><code>u</code></td>
<td>進行 Unicode 搜尋。</td>
</tr>
</tbody>
</table>
<p>用 <code>g</code> 修飾子在字串的 <code>replace</code> 方法中,可以在整個字串中進行取代。</p>
<p>樣式本身可能包含</p>
<table>
<thead>
<tr>
<th>樣式</th>
<th>意義</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>[ab]</code></td>
<td>比對 <code>[]</code> 中的字元。</td>
</tr>
<tr>
<td><code>[a-z]</code></td>
<td>比對 <code>[]</code> 中的字元區間的其中一個字元。</td>
</tr>
<tr>
<td><code>\d</code></td>
<td>比對一個數字字元。</td>
</tr>
<tr>
<td><code>\s</code></td>
<td>比對一個空白字元(包含 tab 等)</td>
</tr>
<tr>
<td><code>\b</code></td>
<td>比對單字的起始或是結尾。</td>
</tr>
<tr>
<td><code>\uxxxx</code></td>
<td>比對一個 Unicode 字元。</td>
</tr>
<tr>
<td><code>x+</code></td>
<td>比對一個以上的 <code>x</code>。</td>
</tr>
<tr>
<td><code>x*</code></td>
<td>比對空字串或是一個以上的 <code>x</code>。</td>
</tr>
<tr>
<td><code>x?</code></td>
<td>比對空字串或是一個 <code>x</code>。</td>
</tr>
</tbody>
</table>
<p>更多資訊:<a href="http://www.w3schools.com/jsref/jsref_obj_regexp.asp">http://www.w3schools.com/jsref/jsref_obj_regexp.asp</a></p>
</div><div class="cell markdown-cell"><h2 id="-">錯誤處理</h2>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">try</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">throw</span> <span class="ace_keyword">new</span> <span class="ace_variable ace_language">Error</span><span class="ace_paren ace_lparen">(</span><span class="ace_string">'<span class="ace_cjk" style="width:20px">錯</span><span class="ace_cjk" style="width:20px">誤</span><span class="ace_cjk" style="width:20px">訊</span><span class="ace_cjk" style="width:20px">息</span>'</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span> <span class="ace_keyword">catch</span> <span class="ace_paren ace_lparen">(</span><span class="ace_identifier">error</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">console</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function ace_firebug">error</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">error</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span> <span class="ace_keyword">finally</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">console</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function ace_firebug">log</span><span class="ace_paren ace_lparen">(</span><span class="ace_string">'finally <span class="ace_cjk" style="width:20px">中</span><span class="ace_cjk" style="width:20px">的</span><span class="ace_cjk" style="width:20px">內</span><span class="ace_cjk" style="width:20px">容</span><span class="ace_cjk" style="width:20px">一</span><span class="ace_cjk" style="width:20px">定</span><span class="ace_cjk" style="width:20px">會</span><span class="ace_cjk" style="width:20px">被</span><span class="ace_cjk" style="width:20px">執</span><span class="ace_cjk" style="width:20px">行</span><span class="ace_cjk" style="width:20px">。</span>'</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-">嚴格模式</h2>
<p>在 JavaScript 的第一行寫 "use strict" 可以啟用嚴格模式,直譯器將會更嚴格的執行程式碼檢查。也可以只為一個函數啟用嚴格模式。</p>
</div><div class="cell markdown-cell"><h2 id="-for-in-"><code>for...in</code> 迴圈</h2>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">object</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">a</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">b</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_constant ace_numeric">2</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">for</span> <span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">property</span> <span class="ace_keyword">in</span> <span class="ace_identifier">object</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">console</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function ace_firebug">log</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">property</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">object</span><span class="ace_paren ace_lparen">[</span><span class="ace_identifier">property</span><span class="ace_paren ace_rparen">])</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-">[ES6] 箭頭匿名函數</h2>
<p>許多 JavaScript 的內建函數必須傳入函數</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">array</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">3</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">4</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">5</span><span class="ace_paren ace_rparen">]</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">function</span> <span class="ace_entity ace_name ace_function">transformFunction</span><span class="ace_paren ace_lparen">(</span><span class="ace_variable ace_parameter">value</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_identifier">value</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_constant ace_numeric">5</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">newArray</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">array</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">map</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">transformFunction</span><span class="ace_paren ace_rparen">)</span> <span class="ace_comment">// [6, 7, 8, 9, 10]</span>
</div></div></div></div><div class="cell markdown-cell"><p>若使用匿名函數,可以讓程式碼更簡潔</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">array</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">3</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">4</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">5</span><span class="ace_paren ace_rparen">]</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">newArray</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">array</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">map</span><span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">value</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_identifier">value</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_constant ace_numeric">5</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">})</span>
</div></div></div></div><div class="cell markdown-cell"><p>箭頭匿名函數提供一個更簡潔的寫法</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">array</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">3</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">4</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">5</span><span class="ace_paren ace_rparen">]</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">newArray</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">array</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">map</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">value</span> <span class="ace_keyword ace_operator">=></span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_identifier">value</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_constant ace_numeric">5</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">})</span>
</div></div></div></div><div class="cell markdown-cell"><p>若匿名函數本身只有一個回傳敘述,可以化簡為</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">array</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">3</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">4</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">5</span><span class="ace_paren ace_rparen">]</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">newArray</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">array</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">map</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">value</span> <span class="ace_keyword ace_operator">=></span> <span class="ace_identifier">value</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_constant ace_numeric">5</span><span class="ace_paren ace_rparen">)</span>
</div></div></div></div><div class="cell markdown-cell"><p>箭頭函數若沒有參數或是有多個參數則必須用括號將參數列表括起來</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">array</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">3</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">4</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">5</span><span class="ace_paren ace_rparen">]</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">sum</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">array</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">reduce</span><span class="ace_paren ace_lparen">((</span><span class="ace_identifier">partialSum</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">value</span><span class="ace_paren ace_rparen">)</span> <span class="ace_keyword ace_operator">=></span> <span class="ace_identifier">partialSum</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_identifier">value</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">one_factory</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_keyword ace_operator">=></span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_constant ace_numeric">1</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-">[ES6] 類別</h2>
<p>雖然 ECMAScript 6 的類別與 ECMAScript 5 的類別一樣是使用原型,但在 ECMAScirpt 6 中,可以用與 C++ 類似的方法。</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">class</span> <span class="ace_identifier">Base</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">constructor</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">value</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">value</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">value</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">class</span> <span class="ace_identifier">Derived</span> <span class="ace_keyword">extends</span> <span class="ace_identifier">Base</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">constructor</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">super</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">5</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">factory</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">Derived</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">defaultFactory</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">get</span> <span class="ace_identifier">factoryFunction</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_paren ace_lparen">(</span><span class="ace_identifier">value</span><span class="ace_paren ace_rparen">)</span> <span class="ace_keyword ace_operator">=></span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">value</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">factory</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">set</span> <span class="ace_identifier">specification</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">specification</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_variable ace_language">this</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">factory</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">specification</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">static</span> <span class="ace_identifier">defaultFactory</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_constant ace_numeric">0</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-">[ES6] 增強的物件常值</h2>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">factoryDefault</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">0</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">object</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">__proto__</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_identifier">Base</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_constant">prototype</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">factoryDefault</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_comment">// <span class="ace_cjk" style="width:20px">等</span><span class="ace_cjk" style="width:20px">義</span><span class="ace_cjk" style="width:20px">於</span> factoryDefault: factoryDefault</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">toString</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_string">'Base'</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_lparen">[</span><span class="ace_string">'a'</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_string">'b'</span><span class="ace_paren ace_rparen">]</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_constant ace_numeric">41</span> <span class="ace_comment">// ab: 41</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-">[ES6] 樣板字串</h2>
<p>樣板字串可以用來建立多行的字串。</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">str</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string ace_quasi ace_start">`</span><span class="ace_string ace_quasi">Multiline</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_string ace_quasi">string is now possible.</span><span class="ace_string ace_quasi ace_end">`</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">x</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">2</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">str</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string ace_quasi ace_start">`</span><span class="ace_paren ace_quasi ace_start">${</span><span class="ace_identifier">x</span><span class="ace_paren ace_quasi ace_end">}</span><span class="ace_string ace_quasi ace_end">`</span> <span class="ace_comment">// "2"</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-">[ES6]解構</h2>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">array</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">3</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">4</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">5</span><span class="ace_paren ace_rparen">]</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_paren ace_lparen">[</span><span class="ace_identifier">one</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">two</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">three</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">four</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">five</span><span class="ace_paren ace_rparen">]</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">array</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">object</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">a</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">b</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_constant ace_numeric">2</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">a</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_identifier">_one</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">b</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_identifier">_two</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">object</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// <span class="ace_cjk" style="width:20px">用</span><span class="ace_cjk" style="width:20px">來</span><span class="ace_cjk" style="width:20px">實</span><span class="ace_cjk" style="width:20px">作</span><span class="ace_cjk" style="width:20px">函</span><span class="ace_cjk" style="width:20px">數</span><span class="ace_cjk" style="width:20px">的</span><span class="ace_cjk" style="width:20px">有</span><span class="ace_cjk" style="width:20px">名</span><span class="ace_cjk" style="width:20px">參</span><span class="ace_cjk" style="width:20px">數</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">function</span> <span class="ace_entity ace_name ace_function">f</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">parameter1</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_identifier">a</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">parameter2</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_identifier">b</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">})</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_identifier">a</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_identifier">b</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">f</span><span class="ace_paren ace_lparen">({</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">parameter2</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">parameter1</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_constant ace_numeric">2</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">})</span>
</div></div></div></div><div class="cell markdown-cell"><p>安全性,若解構的資料大小有可能不一樣,你應該使用</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_paren ace_lparen">[</span><span class="ace_identifier">a</span><span class="ace_paren ace_rparen">]</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_paren ace_rparen">]</span> <span class="ace_comment">// a === undefined</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_paren ace_lparen">[</span><span class="ace_identifier">a</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">1</span><span class="ace_paren ace_rparen">]</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_paren ace_rparen">]</span> <span class="ace_comment">// a === 1</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-">[ES6] 函數參數</h2>
<p>函數參數可以有預設值(<code>Default</code>)、動態長度(<code>Rest</code>)、或是在呼叫函數的時候將陣列的內容分散(<code>Spread</code>)出去。</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// Default</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">function</span> <span class="ace_entity ace_name ace_function">f_1</span><span class="ace_paren ace_lparen">(</span><span class="ace_variable ace_parameter">x</span><span class="ace_punctuation ace_operator">, </span><span class="ace_variable ace_parameter">y</span><span class="ace_punctuation ace_operator"> </span><span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">12</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_identifier">x</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_identifier">y</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">f_1</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_language">undefined</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">f_1</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">1</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// ES5 Arguments</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">function</span> <span class="ace_entity ace_name ace_function">f_2</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_variable ace_language">arguments</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_constant">length</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">f_2</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">3</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">4</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">5</span><span class="ace_paren ace_rparen">)</span> <span class="ace_comment">// 5</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// Rest</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">function</span> <span class="ace_entity ace_name ace_function">f_3</span><span class="ace_paren ace_lparen">(</span><span class="ace_variable ace_parameter">x</span><span class="ace_punctuation ace_operator">, ..</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_constant">y</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_identifier">x</span> <span class="ace_keyword ace_operator">*</span> <span class="ace_identifier">y</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_constant">length</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">f_3</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">3</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">4</span><span class="ace_paren ace_rparen">)</span> <span class="ace_comment">// 1 * 3</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// Spread</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">array</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">3</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">4</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">5</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">6</span><span class="ace_paren ace_rparen">]</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">f_3</span><span class="ace_paren ace_lparen">(</span><span class="ace_punctuation ace_operator">...</span><span class="ace_identifier">array</span><span class="ace_paren ace_rparen">)</span> <span class="ace_comment">// 2 * 4</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-let-const-">[ES6] <code>let</code> 以及 <code>const</code></h2>
<p>原本 ECMAScript 5 的 <code>var</code> 定義出的是全域變數或是函數範圍的變數(在整個函數內可用)。</p>
<p>ECMAScript 6 加入 <code>let</code>,將該變數的範圍定義在大括號中;以及 <code>const</code> 定義出只能被給值一次。</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_string">"use strict"</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">function</span> <span class="ace_entity ace_name ace_function">f</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">let</span> <span class="ace_identifier">x</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">const</span> <span class="ace_identifier">x</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string">'hello'</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_identifier">x</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string">'foo'</span> <span class="ace_comment">// <span class="ace_cjk" style="width:20px">錯</span><span class="ace_cjk" style="width:20px">誤</span><span class="ace_cjk" style="width:20px">,</span><span class="ace_cjk" style="width:20px">被</span><span class="ace_cjk" style="width:20px">給</span><span class="ace_cjk" style="width:20px">值</span><span class="ace_cjk" style="width:20px">第</span><span class="ace_cjk" style="width:20px">二</span><span class="ace_cjk" style="width:20px">次</span><span class="ace_cjk" style="width:20px">了</span><span class="ace_cjk" style="width:20px">!</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">let</span> <span class="ace_identifier">x</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string">'inner'</span> <span class="ace_comment">// <span class="ace_cjk" style="width:20px">錯</span><span class="ace_cjk" style="width:20px">誤</span><span class="ace_cjk" style="width:20px">,</span><span class="ace_cjk" style="width:20px">被</span><span class="ace_cjk" style="width:20px">定</span><span class="ace_cjk" style="width:20px">義</span><span class="ace_cjk" style="width:20px">第</span><span class="ace_cjk" style="width:20px">二</span><span class="ace_cjk" style="width:20px">次</span><span class="ace_cjk" style="width:20px">了</span><span class="ace_cjk" style="width:20px">!</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-for-of-">[ES6] 迭代器以及 <code>for...of</code> 迴圈</h2>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">let</span> <span class="ace_identifier">fibonacci</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_lparen">[</span><span class="ace_identifier">Symbol</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">iterator</span><span class="ace_paren ace_rparen">]</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">let</span> <span class="ace_identifier">pre</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">0</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">cur</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">1</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_indent-guide"> </span> <span class="ace_identifier">next</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_indent-guide"> </span> <span class="ace_paren ace_lparen">[</span><span class="ace_identifier">pre</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">cur</span><span class="ace_paren ace_rparen">]</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_identifier">cur</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">pre</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_identifier">cur</span><span class="ace_paren ace_rparen">]</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_indent-guide"> </span> <span class="ace_keyword">return</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_indent-guide"> </span><span class="ace_indent-guide"> </span> <span class="ace_identifier">done</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_constant ace_language ace_boolean">false</span><span class="ace_punctuation ace_operator">,</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_indent-guide"> </span><span class="ace_indent-guide"> </span> <span class="ace_identifier">value</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_identifier">cur</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_indent-guide"> </span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_indent-guide"> </span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">for</span> <span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">n</span> <span class="ace_identifier">of</span> <span class="ace_identifier">fibonacci</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">console</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function ace_firebug">log</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">n</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><p>當然,<code>for...of</code> 迴圈也可以用在陣列上</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">let</span> <span class="ace_identifier">array</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">3</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">4</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">5</span><span class="ace_paren ace_rparen">]</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">for</span> <span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">element</span> <span class="ace_identifier">of</span> <span class="ace_identifier">array</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">console</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function ace_firebug">log</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">element</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">for</span> <span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">index</span> <span class="ace_keyword">in</span> <span class="ace_identifier">array</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">console</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function ace_firebug">log</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">array</span><span class="ace_paren ace_lparen">[</span><span class="ace_identifier">index</span><span class="ace_paren ace_rparen">])</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-">[ES6] 產生器</h2>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">fibonacci</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_lparen">[</span><span class="ace_identifier">Symbol</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">iterator</span><span class="ace_paren ace_rparen">]</span><span class="ace_punctuation ace_operator">:</span> <span class="ace_storage ace_type">function</span><span class="ace_keyword ace_operator">*</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">let</span> <span class="ace_identifier">pre</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">0</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">cur</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">1</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">while</span> <span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_language ace_boolean">true</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_indent-guide"> </span> <span class="ace_paren ace_lparen">[</span><span class="ace_identifier">pre</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">cur</span><span class="ace_paren ace_rparen">]</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_paren ace_lparen">[</span><span class="ace_identifier">cur</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">pre</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_identifier">cur</span><span class="ace_paren ace_rparen">]</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_indent-guide"> </span> <span class="ace_keyword">yield</span> <span class="ace_identifier">cur</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-unicode-">[ES6] Unicode 字元</h2>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">let</span> <span class="ace_identifier">x</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string">"<span class="ace_cjk" style="width:20px">吉</span><span class="ace_cjk" style="width:20px">祥</span>"</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">let</span> <span class="ace_identifier">length</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">x</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_constant">length</span> <span class="ace_comment">// 4</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">let</span> <span class="ace_identifier">codePoint</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_identifier">x</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">codePointAt</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">0</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">for</span> <span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">let</span> <span class="ace_identifier">c</span> <span class="ace_identifier">of</span> <span class="ace_identifier">x</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">console</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function ace_firebug">log</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">c</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-">[ES6] 模組</h2>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// lib/math.js</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">export</span> <span class="ace_storage ace_type">function</span> <span class="ace_entity ace_name ace_function">sum</span><span class="ace_paren ace_lparen">(</span><span class="ace_variable ace_parameter">x</span><span class="ace_punctuation ace_operator">, </span><span class="ace_variable ace_parameter">y</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_identifier">x</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_identifier">y</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">export</span> <span class="ace_storage ace_type">let</span> <span class="ace_identifier">pi</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">3.14159</span>
</div></div></div></div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">import</span> <span class="ace_keyword ace_operator">*</span> <span class="ace_identifier">as</span> <span class="ace_identifier">math</span> <span class="ace_identifier">from</span> <span class="ace_string">'lib/math'</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">math</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">sum</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">1</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">2</span><span class="ace_paren ace_rparen">)</span>
</div></div></div></div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_comment">// lib/math2.js</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">export</span> <span class="ace_keyword ace_operator">*</span> <span class="ace_identifier">from</span> <span class="ace_string">'lib/math'</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">export</span> <span class="ace_storage ace_type">const</span> <span class="ace_identifier">e</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">2.71828182846</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">export</span> <span class="ace_keyword">default</span> <span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">x</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_variable ace_language">Math</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function">log</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">x</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div></div></div></div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">import</span> <span class="ace_identifier">ln</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_paren ace_lparen">{</span><span class="ace_identifier">pi</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">e</span><span class="ace_paren ace_rparen">}</span> <span class="ace_identifier">from</span> <span class="ace_string">'lib/math2'</span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-">[ES6] 資料結構</h2>
<p>ECMAScript 6 提供 <code>Map</code>、<code>Set</code>、<code>WeakMap</code> 以及 <code>WeakSet</code>。</p>
<p>其中 <code>Weak</code> 代表當它所包含的資料只由它所擁有,則會被自動釋放。換句話說,它不能是該值的唯一擁有者。</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_keyword">set</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_keyword">new</span> <span class="ace_identifier">Set</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">set</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function ace_dom">add</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">1</span><span class="ace_paren ace_rparen">)</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function ace_dom">add</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">2</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_keyword">set</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">has</span><span class="ace_paren ace_lparen">(</span><span class="ace_constant ace_numeric">1</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">map</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_keyword">new</span> <span class="ace_identifier">Map</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">map</span><span class="ace_punctuation ace_operator">.</span><span class="ace_keyword">set</span><span class="ace_paren ace_lparen">(</span><span class="ace_string">'a'</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">1</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">map</span><span class="ace_punctuation ace_operator">.</span><span class="ace_keyword">get</span><span class="ace_paren ace_lparen">(</span><span class="ace_string">'a'</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">weakmap</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_keyword">new</span> <span class="ace_identifier">WeakMap</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">var</span> <span class="ace_identifier">k</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string">'2'</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">weakmap</span><span class="ace_punctuation ace_operator">.</span><span class="ace_keyword">set</span><span class="ace_paren ace_lparen">(</span><span class="ace_string">'a'</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_constant ace_numeric">1</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">weakmap</span><span class="ace_punctuation ace_operator">.</span><span class="ace_keyword">set</span><span class="ace_paren ace_lparen">(</span><span class="ace_string">'b'</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">k</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">weakmap</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">size</span> <span class="ace_keyword ace_operator">===</span> <span class="ace_constant ace_numeric">1</span> <span class="ace_comment">// <span class="ace_cjk" style="width:20px">因</span><span class="ace_cjk" style="width:20px">為</span> 'a': 1 <span class="ace_cjk" style="width:20px">被</span><span class="ace_cjk" style="width:20px">釋</span><span class="ace_cjk" style="width:20px">放</span></span>
</div></div></div></div><div class="cell markdown-cell"><h2 id="-es6-promise-">[ES6] 保證(Promise)</h2>
<p>保證是一個進行非同步處理的方法。原本的方法</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">http</span><span class="ace_punctuation ace_operator">.</span><span class="ace_keyword">get</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">url</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">data</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">err</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">if</span> <span class="ace_paren ace_lparen">(</span><span class="ace_identifier">err</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_comment">// <span class="ace_cjk" style="width:20px">錯</span><span class="ace_cjk" style="width:20px">誤</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_comment">// <span class="ace_cjk" style="width:20px">處</span><span class="ace_cjk" style="width:20px">理</span><span class="ace_cjk" style="width:20px">資</span><span class="ace_cjk" style="width:20px">料</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">})</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">http</span><span class="ace_punctuation ace_operator">.</span><span class="ace_keyword">get</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">url</span><span class="ace_paren ace_rparen">)</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">then</span><span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">data</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">})</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">then</span><span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">data</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_comment">// <span class="ace_cjk" style="width:20px">可</span><span class="ace_cjk" style="width:20px">以</span><span class="ace_cjk" style="width:20px">有</span><span class="ace_cjk" style="width:20px">多</span><span class="ace_cjk" style="width:20px">個</span><span class="ace_cjk" style="width:20px">處</span><span class="ace_cjk" style="width:20px">理</span><span class="ace_cjk" style="width:20px">常</span><span class="ace_cjk" style="width:20px">式</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">})</span><span class="ace_punctuation ace_operator">.</span><span class="ace_keyword">catch</span><span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">err</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_comment">// <span class="ace_cjk" style="width:20px">利</span><span class="ace_cjk" style="width:20px">用</span> then <span class="ace_cjk" style="width:20px">或</span><span class="ace_cjk" style="width:20px">是</span> catch <span class="ace_cjk" style="width:20px">區</span><span class="ace_cjk" style="width:20px">分</span><span class="ace_cjk" style="width:20px">成</span><span class="ace_cjk" style="width:20px">功</span><span class="ace_cjk" style="width:20px">或</span><span class="ace_cjk" style="width:20px">是</span><span class="ace_cjk" style="width:20px">失</span><span class="ace_cjk" style="width:20px">敗</span></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">})</span><span class="ace_punctuation ace_operator">.</span><span class="ace_keyword">catch</span><span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">err</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">})</span>
</div></div></div></div><div class="cell markdown-cell"><h3 id="-promise">建立 Promise</h3>
<p>假設我們要寫一個複雜的亂數程式,需要一段時間才能算出亂數。而且,若算出來的值為 0,則算是失敗。</p>
</div><div class="cell code-cell"><div class="ace-xcode"><div class="ace_static_highlight ace_show_gutter" style="counter-reset:ace_line 0"><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_storage ace_type">const</span> <span class="ace_entity ace_name ace_function">random</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">return</span> <span class="ace_keyword">new</span> <span class="ace_identifier">Promise</span><span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">function</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">resolve</span><span class="ace_punctuation ace_operator">,</span> <span class="ace_identifier">reject</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_storage ace_type">let</span> <span class="ace_identifier">value</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">5</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_keyword">if</span> <span class="ace_paren ace_lparen">(</span><span class="ace_identifier">value</span> <span class="ace_keyword ace_operator">==</span> <span class="ace_constant ace_numeric">0</span><span class="ace_paren ace_rparen">)</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_indent-guide"> </span> <span class="ace_identifier">reject</span><span class="ace_paren ace_lparen">(</span> <span class="ace_keyword">new</span> <span class="ace_variable ace_language">Error</span><span class="ace_paren ace_lparen">(</span><span class="ace_string">'<span class="ace_cjk" style="width:20px">計</span><span class="ace_cjk" style="width:20px">算</span><span class="ace_cjk" style="width:20px">失</span><span class="ace_cjk" style="width:20px">敗</span>'</span><span class="ace_paren ace_rparen">))</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span> <span class="ace_keyword">else</span> <span class="ace_paren ace_lparen">{</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_indent-guide"> </span> <span class="ace_identifier">resolve</span><span class="ace_paren ace_lparen">(</span><span class="ace_identifier">value</span><span class="ace_paren ace_rparen">)</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span> <span class="ace_paren ace_rparen">})</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_paren ace_rparen">}</span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span>
</div><div class="ace_line"><span class="ace_gutter ace_gutter-cell" unselectable="on"></span><span class="ace_identifier">random</span><span class="ace_paren ace_lparen">(</span><span class="ace_paren ace_rparen">)</span><span class="ace_punctuation ace_operator">.</span><span class="ace_identifier">then</span><span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">console</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function ace_firebug">log</span><span class="ace_paren ace_rparen">)</span><span class="ace_punctuation ace_operator">.</span><span class="ace_keyword">catch</span><span class="ace_paren ace_lparen">(</span><span class="ace_storage ace_type">console</span><span class="ace_punctuation ace_operator">.</span><span class="ace_support ace_function ace_firebug">error</span><span class="ace_paren ace_rparen">)</span>
</div></div></div></div></div>
<script>document.body.onkeyup = function(e) {
if (e.keyCode === 39) window.location.href = 'Node.js.html';
if (e.keyCode === 37) window.location.href = '技術簡介.html';
}</script>
</body>
</html>