View
252
Download
9
Embed Size (px)
DESCRIPTION
이노우에 세이이치로, 츠치에 타쿠로, 하마베 쇼타 지음 | 황선유 옮김 | 오픈소스 & 웹 시리즈 _ 047 | ISBN: 9788998139131 | 32,000원 | 2013년 02월 14일 발행 | 740쪽
Citation preview
iv
[1]
01 | 1
1-1 .........................................................................1
1-2 ..................................................................................2
1-2-1 .............................................3
1-3 ECMAScript ...............................................................................................4
1-3-1 ...............................................................4
1-3-2 ECMAScript 4 .................................................................5
1-4 ..................................................................................6
1-5 .....................................................................................7
1-5-1 .....................7
1-6 .............................................................................9
1-6-1 .......................................................................................9
1-6-2 .................................................................................10
1-7 ........................................................ 11
1-7-1 ..................................................................................11
1-7-2 .............................................................................12
1-7-3 ............................................................................13
v[2]
02 | 17
2-1 ............................................................................... 17
2-2 ........................................................................................................... 19
2-2-1 print ....................................................................................20
2-3 .............................................................................................. 21
2-3-1 .................................................................................21
2-3-2 var ...................................................................................22
2-3-3 ..............................................................................................23
2-4 .............................................................................................. 24
2-4-1 ? ........................................................................................24
2-4-2 ........................................................................25
2-4-3 .................................................................................26
2-4-4 .................................................................................28
2-5 .............................................................................................. 28
2-5-1 ? ........................................................................................28
2-5-2 ..................................................29
2-5-3 .............................................................................30
2-5-4 () ..............................................................31
2-5-5 ..........................................................................................31
vi
2-5-6 new ..............................................................................................32
2-5-7 .....................................................................32
2-5-8 ....................................................................33
2-5-9 .................................................................................34
2-6 .............................................................................................. 34
03 | 37
3-1 ? .................................................................................................. 37
3-1-1 .......................................................38
3-1-2 ...............................................................39
3-2 ..................................................................................... 39
3-2-1 ........................................................39
3-3 .............................................................................................. 40
3-3-1 ........................................................................40
3-3-2 ....................................................................42
3-3-3 ....................................................................43
3-3-4 (String ) ..................................................45
3-3-5 .................................................................................45
3-3-6 .....................47
3-3-7 String .........................................................................48
3-3-8 String .................................................................48
3-3-9 .........................................................................50
3-4 .................................................................................................. 51
3-4-1 .................................................................................51
3-4-2 ........................................................................52
3-4-3 .......................................53
3-4-4 (Number ) ..................................................54
3-4-5 Number .....................................................................55
vii
3-4-6 Number .............................................................55
3-4-7 ................................................................57
3-4-8 NaN .............................................................................................58
3-5 .................................................................................................. 61
3-5-1 .........................................................................................61
3-5-2 (Boolean ) ...................................................62
3-5-3 Boolean ..............................................................63
3-6 null ................................................................................................... 64
3-7 undened ........................................................................................ 65
3-7-1 undened ...............................................................................65
3-8 .................................................................................................. 67
3-8-1 .....................................................................................67
3-9 .................................................................................................. 68
3-9-1 ...............................................68
3-9-2 ...........................................70
3-9-3 .........................................................................71
3-9-4 ............................................................72
3-9-5 ............................................................................74
3-9-6 ...................................74
3-9-7 ...................................75
04 | , , 77
4-1 ..................................................................................... 77
4-2 ....................................................................................................... 78
4-3 ....................................................................................................... 78
4-4 .............................................................................................. 80
4-5 ? .................................................................................................. 81
4-6 () ........................................................................................ 82
viii
4-7 .............................................................................................. 82
4-8 .............................................................................................. 82
4-9 ....................................................................................................... 83
4-10 ...................................................................................................... 84
4-11 ....................................................................................................... 85
4-12 if-else ................................................................................................... 85
4-13 switch-case .......................................................................................... 89
4-14 ....................................................................................................... 92
4-15 while ..................................................................................................... 93
4-16 do-while .............................................................................................. 96
4-17 for ......................................................................................................... 97
4-17-1 for ...........................................................................98
4-18 for in .................................................................................................. 100
4-18-1 for in ........................................................................101
4-18-2 for in .......................................................102
4-19 for each in ......................................................................................... 102
4-20 break .................................................................................................. 103
4-21 continue ............................................................................................. 104
4-22 ............................................................................... 105
4-23 return ................................................................................................. 106
4-24 ......................................................................................................... 107
4-25 ......................................................................................................... 109
4-26 ......................................................................................................... 110
4-27 ............................................................................................................. 110
4-28 ..................................................................................................... 110
4-29 ................................................................................................ 112
4-30 ........................................................... 113
4-31 ............................................................................................ 114
ix
4-32 ............................................................................... 116
4-33 ............................................................................................ 116
4-34 ............................................................................................ 118
4-35 in ................................................................................................. 120
4-36 instanceof .................................................................................. 120
4-37 ............................................................................................ 120
4-38 ............................................................................................ 122
4-39 ............................................................................................ 123
4-40 ................................................................................... 124
4-41 ( )..................................................................... 124
4-42 typeof ......................................................................................... 124
4-43 new ............................................................................................. 125
4-44 delete .......................................................................................... 125
4-45 void ............................................................................................. 126
4-46 (,) ........................................................................................ 127
4-47 ................................................................... 127
4-48 ................................................................................... 128
4-49 .................................................................... 128
05 | 129
5-1 ............................................................................................ 129
5-2 ............................................................................................ 130
5-2-1 ( ) ...............................................................133
5-2-2 ...........................................................................134
5-2-3 ............................................135
5-3 .................................................................................... 135
5-4 ........................................................................................ 137
x5-5 ................................................................................... 138
5-5-1 ..............................................................139
5-6 ..................................................................................................... 140
5-6-1 .................................................140
5-6-2 ..................................................141
5-6-3 ...............................................................141
5-7 ............................................................................................ 142
5-7-1 ...............................................................................142
5-7-2 new .......................................................................146
5-7-3 ..............................................................148
5-8 ........................................................................................ 149
5-8-1 ..................................................................150
5-8-2 ................................150
5-8-3 .......................................................................152
5-9 ............................................................................ 152
5-9-1 ...................................................................................153
5-9-2 .................................154
5-10 .................................................................................... 156
5-11 ........................................................................................ 157
5-12 ................................................................................................ 158
5-12-1 ? .............................................................................158
5-12-2 .................................................................158
5-12-3 ......................................................................158
5-13 ..................................................................................................... 161
5-14 this .................................................................................................. 161
5-14-1 this .......................................................................162
5-14-2 this ...................................................................163
5-15 apply call ............................................................................................. 165
xi
5-16 .................................................................................... 166
5-16-1 .......................................................................167
5-16-2 .............................................169
5-16-3 ......................................................171
5-16-4 __proto__ .................................................................... 172
5-16-5 .......................................................................172
5-16-6 ECMAScript 5 ........................................173
5-17 ............................................................................................ 174
5-17-1 (constructor ) ...........................................174
5-17-2 constructor ..............................................175
5-17-3 (instanceof isPrototypeOf ) ...........176
5-17-4 ( ) ...............................................................177
5-17-5 ( )...........................177
5-18 ECMAScript 5 Object .......................................................... 179
5-18-1 ...........................................................................180
5-18-2 ..........................................................................181
5-19 ................................................................................................ 184
5-20 Object ........................................................................................ 185
5-21 ................................................................................................ 187
5-21-1 .............................................................189
5-21-2 Math .................................................................................190
5-21-3 Error .................................................................................191
xii
06 | 193
6-1 .............................................................. 193
6-2 ................................................................................... 193
6-2-1 ..................................................................194
6-3 ................................................................................... 195
6-3-1 arguments ........................................................................195
6-3-2 ...................................................................................196
6-4 ..................................................................................................... 197
6-4-1 ..............................................................199
6-4-2 ...............................................................................199
6-4-3 let ......................................................................200
6-4-4 ..................................................................203
6-4-5 ........................................................................................204
6-5 ............................................................................................ 205
6-5-1 ..........................................................207
6-6 Function .................................................................................... 208
6-6-1 Function ..........................................................209
6-7 ....................................................................... 210
6-7-1 ..........................................................210
6-7-2 ...........................................................................211
6-7-3 ...........................................................................216
6-7-4 ......................................................217
6-7-5 .......................................................................221
6-8 ................................................................................................ 223
6-8-1 ..................................................................223
6-8-2 ...............................................................225
xiii
07 | 229
7-1 ......................................................................................................... 229
7-1-1 ...............................................................229
7-1-2 ......................................................................231
7-1-3 ...............................................................................232
7-1-4 ......................................................................234
7-1-5 ...............................................................................235
7-1-6 ...............................................................................236
7-1-7 Array .............................................................................238
7-1-8 ......................................................................240
7-1-9 ...........................................................................241
7-1-10 ...............................................................................246
7-1-11 ..................................................................248
7-1-12 ................................................................................249
7-1-13 ................................................................................251
7-1-14 ...............................................................................254
7-2 JSON ....................................................................................................... 255
7-2-1 JSON .............................................................................255
7-2-2 JSON .................................................................................256
7-3 ................................................................................................ 258
7-3-1 Date ...............................................................................259
7-4 ............................................................................................. 262
7-4-1 ? ......................................................................262
7-4-2 ...................................................................263
7-4-3 ...................................................................264
7-4-4 ...................................................266
7-4-5 ...........................................................268
7-4-6 .............................................270
xiv
[3]
08 | HTML 277
8-1 ....................................... 277
8-1-1 ..........................................................277
8-1-2 ...........................................................278
8-1-3 ...............................................................279
8-2 HTML ........................................................................ 279
8-2-1 .......................................279
8-2-2 .............................280
8-2-3 ......................................................................285
8-3 ........................................................................... 286
8-3-1 ...................................................................................286
8-3-2 ...................................................................................286
8-4 ..................................................................................................... 286
8-4-1 alert ............................................................................................286
8-4-2 console ......................................................................................287
8-4-3 onerror ......................................................................................291
8-4-4 , , ...................291
8-5 ............................................................................ 294
8-5-1 ..............................................................295
8-5-2 ...................................................................................296
8-6 Window ......................................................................................... 299
8-6-1 Navigator ..........................................................................300
8-6-2 Location ...........................................................................300
8-6-3 History .............................................................................302
8-6-4 Screen ...............................................................................302
8-6-5 Window ...........................................................303
8-6-6 Document ........................................................................304
xv
09 | DOM 305
9-1 DOM? ............................................................................................. 305
9-1-1 DOM Level 1 ............................................................................306
9-1-2 DOM Level 2 ............................................................................306
9-1-3 DOM Level 3 ............................................................................307
9-1-4 DOM ............................................................................308
9-2 DOM .......................................................................................... 308
9-2-1 , , ........................................................................308
9-2-2 DOM ................................................................................309
9-2-3 Document ........................................................................309
9-3 ............................................................................................ 310
9-3-1 ID .........................................................................310
9-3-2 ..................................................................311
9-3-3 ......................................................................317
9-3-4 ..............................................................317
9-3-5 , , ........................................................................318
9-3-6 XPath .........................................................................................321
9-3-7 API ................................................................................325
9-4 / ................................................................................... 327
9-5 ................................................................................... 327
9-6 ............................................................................................ 328
9-7 innerHTML/textContent ..................................................................... 328
9-7-1 innerHTML ..............................................................................328
9-7-2 textContent ...............................................................................329
9-8 DOM ................................................................................. 329
xvi
10 | 331
10-1 .................................................................... 331
10-2 / ................................................. 332
10-2-1 HTML .............................................332
10-2-2 DOM .......................................334
10-2-3 EventTarget.addEventListener() .......................335
10-2-4 / this .........................338
10-3 ........................................................................................ 339
10-4 ........................................................................................ 339
10-4-1 ...............................................................................340
10-4-2 ...................................................................................340
10-4-3 ...............................................................................340
10-4-4 ...................................................................................341
10-5 ............................................................................... 342
10-6 ............................................................................................ 343
10-6-1 DOM Level 2 .....................................343
10-6-2 DOM Level 3 .....................................345
10-7 ............................................................................................ 349
11 | 351
11-1 ..................................................................................................... 351
11-1-1 ......................................................................352
11-1-2 ...................................................................................358
11-1-3 ............................................................................................359
11-1-4 ................................................................................362
11-2 AJAX ....................................................................................................... 362
11-2-1 ..................................................................363
xvii
11-2-2 XMLHttpRequest ....................................................................363
11-2-3 ..................................................................364
11-2-4 ...................................................................................366
11-2-5 ....................................................................................366
11-2-6 ............................................................................................368
11-2-7 ..........................................................................369
11-2-8 ..................................................................370
11-2-9 JSONP .......................................................................................371
11-2-10 iframe ...............................................................................372
11-2-11 window.postMessage ...............................................................376
11-2-12 XMLHttpRequest Level 2 .......................................................377
11-2-13 ............................................378
11-3 ............................................................................................................. 378
11-3-1 .......................................................................................379
11-3-2 ..........................................................................381
11-3-3 ...................................................................................382
11-3-4 .......................................383
11-3-5 ...............384
12 | 387
12-1 ....................................................... 387
12-2 jQuery ........................................................................................ 388
12-3 jQuery ........................................................................................ 389
12-3-1 .......................................................................................389
12-3-2 ...............................................................................391
12-4 $ ...................................................................................................... 393
12-4-1 .....................................393
12-4-2 DOM ...............................................393
xviii
12-4-3 DOM jQuery .....................394
12-4-4 DOM .........................394
12-5 jQuery DOM .................................................................... 394
12-5-1 ...............................................................................394
12-5-2 .................................................398
12-6 jQuery .................................................................. 399
12-6-1 ....................................................399
12-6-2 ................................401
12-6-3 ready() ..........................................................................403
12-7 jQuery .................................................................. 403
12-7-1 ..............................................................403
12-7-2 ................................................................................405
12-8 jQuery AJAX ............................................................................. 408
12-8-1 AJAX() ..............................................................................408
12-8-2 AJAX() .................................................................410
12-8-3 ...............................................................................410
12-9 Deferred ................................................................................................. 411
12-9-1 Deferred .......................................................................411
12-9-2 ...................................................................................412
12-9-3 ...................................................................................414
12-9-4 ...................................................................................417
12-10 jQuery .................................................................................... 418
12-10-1 jQuery ..........................................................418
12-10-2 jQuery ..............................................................419
12-11 .................................................................... 421
12-11-1 $ ............................................................................421
12-11-2 $ ...................................................................422
12-12 ............................................................................. 422
xix
[4] HTML5
13 | HTML5 427
13-1 HTML5 ...................................................................................... 427
13-1-1 HTML5 ................................................................427
13-2 HTML5 .................................................................................. 429
13-2-1 ..............................................................429
13-2-2 ..........................430
13-3 HTML5 ...................................................................................... 431
14 | 435
14-1 API ......................................................................................... 435
14-1-1 API? ......................................................................435
14-1-2 .......................................................................436
14-1-3 ................................................................................438
14-2 ................................................................................. 444
14-2-1 ...................................................................................444
14-2-2 .......................................................................445
14-2-3 API ............................................................449
14-2-4 ...................................................................452
xx
15 | 453
15-1 API .................................................................................... 453
15-1-1 API? ................................................................453
15-1-2 ................................................................................455
15-1-3 .........................................................457
15-1-4 ...............................................................459
15-1-5 Drag-in/Drag-out .......................................................462
15-2 API ................................................................................................. 466
15-2-1 API? ..............................................................................466
15-2-2 File ....................................................................................466
15-2-3 FileReader .................................................................................468
15-2-4 data URL ...................................................................................473
15-2-5 FileReaderSync.........................................................................476
16 | 479
16-1 ............................................................................................ 479
16-1-1 ? .........................................................................479
16-1-2 ...................................................................................481
16-1-3 storage ..........................................................................484
16-1-4 Cookie ..........................................................................485
16-1-5 ...................................................................486
16-1-6 ...................................................................................487
16-1-7 localStorage ......................................................488
16-2 ............................................................................. 489
16-2-1 ?..........................................................489
16-2-2 ........................................................................490
16-2-3 ...............................................................491
xxi
16-2-4 ..................................................................492
16-2-5 .......................................................493
16-2-6 ...............................................................................495
16-2-7 ..................................................................496
16-2-8 ...............................................................................497
16-2-9 ....................................................................................498
16-2-10 API ....................................................................................499
17 | 501
17-1 ............................................................................................ 501
17-1-1 ? ..............................................................................501
17-1-2 ......................................................................502
17-1-3 ...........................................................................506
17-1-4 ..................................................................507
17-2 ................................................................................................ 507
17-2-1 ...................................................................................507
17-2-2 ...........................................................................508
17-2-3 ...................................................................................509
17-2-4 ..........................................................................510
17-2-5 ......................................................511
17-2-6 WebSocket ..................................512
17-3 ............................................................................................ 512
17-3-1 Node.js .........................................................................513
17-3-2 ..................................................................514
17-3-3 ......................................................515
17-3-4 2 ...................................................517
xxii
18 | 521
18-1 ........................................................................................... 521
18-1-1 ? .................................................................................521
18-1-2 ..........................................................................522
18-2 ............................................................................................ 523
18-2-1 ...............................................................................523
18-2-2 ............................................524
18-2-3 .........................................................524
18-2-4 ...............................................................................525
18-2-5 ..........................................................................526
18-3 ........................................................................................... 527
18-3-2 ..............................................................529
18-4 ................................................................................................ 532
18-4-1 ? .............................................................................532
18-4-2 ......................................................................532
18-4-3 .....................................................533
18-4-4 ......................................................................535
18-4-5 .................................................................535
[5] API
19 | API 541
19-1 API ............................................................................... 541
19-1-1 API ......................................................542
19-2 API ........................................................................................ 543
19-2-1 ................................................................................543
xxiii
19-2-2 ...................................................................................544
19-2-3 XML...........................................................................................544
19-2-4 Atom ..........................................................................................545
19-2-5 JSON ..........................................................................................546
19-2-6 SOAP .........................................................................................546
19-2-7 REST ..........................................................................................547
19-2-8 ...............................................................................548
19-3 API ........................................................................................ 548
19-3-1 API ...........................................................................548
19-3-2 API ...........................................................................550
19-3-3 RESTful API .............................................................................551
19-3-4 API ........................................................................................552
19-4 ............................................................................... 553
19-4-1 .................................................553
19-4-2 .........................................................555
19-4-3 API ...........................................................................556
19-4-4 ...............................................................................559
19-4-5 OAuth ........................................................................................560
20 | API 563
20-1 API ................................................................................ 563
20-2 API ........................................................................................ 565
20-2-1 ............................................................................................566
20-2-2 ...................................................................................566
20-2-3 API ..........................................................568
20-2-4 (Google Translate Element) ...........................................570
20-3 API ........................................................................................ 572
20-3-1 API ..................................................................572
20-3-2 ........................................................................................573
xxiv
20-3-3 API ..............................................................574
20-3-4 API .....................................................574
20-3-5 ........................................................................................576
20-3-6 API API .......................................580
20-4 ! ........................................................................................... 581
20-4-1 API ..............................................................583
20-4-2 API .........................................................585
20-5 ..................................................................................................... 586
20-5-1 API ....................................................................................587
20-5-2 REST API ..................................................................................588
20-5-3 API @anywhere ..............................590
20-5-4 ...............................................................................593
20-6 ................................................................................................. 594
20-6-1 ..............................................594
20-6-2 API ...............................................597
20-6-3 ...................................................................598
20-7 ................................................................................................. 600
20-7-1 ......................................................600
[6]
21 | Node.js 611
21-1 ....................................................... 611
21-2 CommonJS ............................................................................................. 612
21-2-1 CommonJS? ..........................................................................612
21-2-2 CommonJS ..................................................................613
21-2-3 ...................................................................................614
xxv
21-3 Node.js .................................................................................................... 617
21-3-1 Node.js ............................................................................617
21-3-2 node ..............................................................................624
21-3-3 npm ...........................................................................625
21-3-4 console ..............................................................................626
21-3-5 util .....................................................................................627
21-3-6 process ..............................................................................629
21-3-7 ...................................................................................630
21-3-8 Node.js .....................................................631
21-3-9 API ................................................................................633
21-3-10 ............................................................................................640
21-3-11 ........................................................................................646
22 | Node.js 651
22-1 HTTP .................................................................................... 651
21-1-1 HTTP ...........................................................652
22-1-2 ...................................................................................653
22-1-3 ...................................................................................655
22-1-4 POST .......................................................................657
22-2 HTTP ........................................................................ 658
22-3 HTTPS ........................................................................................... 660
22-3-1 openssl .....660
22-3-2 HTTPS .............................................................................660
22-4 Socket.IO ............................................................................... 661
22-5 ................................................................ 663
22-5-1 ..............................................................663
22-5-2 ? ..................................................................................664
22-5-3 .................................................665
22-5-4 .............................................667
xxvi
22-6 ................................................................................................ 670
22-6-1 ...................................................................................670
22-6-2 ..................................................................671
22-6-3 ......................................................................672
22-6-4 .................................................................672
22-6-5 ...................................................................................673
22-6-6 ...................................................................................675
22-6-7 ...........................................................................676
22-6-8 ......................................................................677
22-6-9 ...................................................................................678
22-7 ..................................................................................................... 678
22-8 ............................................................................................. 679
22-8-1 URL ..............................................................................680
22-8-2 ...................................................................................681
22-8-3 ...................................................................................682
22-8-4 ...........................................................................683
22-8-5 MVC .........................................................................683
22-8-6 .................................................................684
22-8-7 () .........................................................688
22-8-8 ..................................................................690
22-8-9 .................692
................................................................................697
xxvii
,
.
.
.
.
.
.
.
.
. ,
.
, .
.
.
.
. .
.
, .
,
.
-
xxviii
.
, (Node.js),
GUI (Gjs, Gnome Seed,
(Titanium Mobile). ,
.
. . this
.
. , 3
. alert('Good morning');
, .
. . ,
.
-
xxix
.
,
Ajax . Ajax
. ,
. .
, ,
, ,
.
.
.
.
-
xxx
.
.
,
, HTML5, API , .
, .
. , UX
.
. PHP
.
.
2 .
.
3 . HTML5 Node.js
. ,
.
.
.
.
.
.
xxxi
y ,
y
y
y
Perf
ect
Java
Scri
pt
1
1
.
101
1-1
2 ,
.
.
.
.
.
,
. .
1 ECMAScript .
, ,
.
2 l
.
.
.
,
.
.
.
.
6
.
.
1-2
1995 .
.
HTML
.
. .
(Hot
Java) .
.
(LiveScript)
, .
.
1 l 3
. (Ruby) (Python)
(Lisp) .
.
1-2-1
( 1.1). ECMAScript .
1.1
1995
1996 JScript
1997 ECMAScript 1 (ECMA-262)
1998 ISO/IEC 16262
1998 ECMAScript 2
1998 DOM 1
1998 DHTML
1999 ECMAScript 3
2000 DOM 2
2002 ISO/IEC 16262:2002
2004 DOM 3
2005 AJAX
2009 ECMAScript 5
2009 HTML5
. PC
.
.
. .
, .
4 l
.
DHTML( HTML)
. DOM CSS W3C
DHTML.
2000 .
, 2000
AJAX(Asynchronous JavaScript and XML)
.
. prototype.js jQuery .
2000 .
. (Mozilla Foundation)
. .
(Firefox)
. 2008
(Google Chrome) v8
. .
1-3 ECMAScript
1-3-1
1-2
. , JScript (Internet Explorer)
. JScript
.
Ecma International .
1 l 5
ECMAScript.
.
. ,
.
ECMAScript ECMA-262. , ISO (ISO-
16262). ISO . ECMAScript
ECMAScript
. JScript .
.
ECMAScript .
.
. ,
ECMAScript .
ECMAScript .
ECMAScript
.
1-3-2 ECMAScript 4
1.1( ) ECMAScript 4 . ECMAScript 4
.
ECMAScript 3 1999. 10
. 10
. 1999 ECMAScript 3 1.5
.
.
.
10 (1999 2008) ECMAScript 4 .
6 l
. ECMAScript 4
. 2008
, ECMAScript 3
. 2009 ECMAScript 5. ECMAScript 5
ECMAScript 3 .
ECMAScript 5 1.6
. ECMAScript 5 . ECMAScript
. ,
.
1-4
ECMAScript
. , ECMAScript
. (
) .
1.2 ECMAScript .
1.2
ECMAScript
1.0 2.0 -
1.1 3.0 ECMAScript
1.2 4.0~4.05 ECMAScript 1
1.3 4.06~4.7x ECMAScript 1
1.4 - ECMAScript 1
1.5 6.0, ECMAScript 3
1.6 1.5 ECMAScript 4
1.7 2 ECMAScript 4
1.8 3 ECMAScript 4
1 l 7
ECMAScript
1.8.1 3.5 1.0 5
1.8.5 4.0 5
1-5
1.3
.
.
1.3
SpiderMonkey
Jscript
JavaScriptCore
v8
Carakan( )
1-5-1
.
1-2
. .
,
.
.
,
C C++ .
1 (OS) . PHP, , ,
1 C++ .
8 l
. .
.
.
.
y
y (DOM CSS )
.
ECMAScript. ECMAScript
.
ECMAScript ECMAScript
.
.
Acid .
http://www.webstandards.org/action/acid3/
Acid ECMAScript
.
, DOM, CSS
. Acid (Acid )
. Acid 3. URL
.
http://acid3.acidtests.org/
. .
. ECMAScript , Acid
, .
.
1 l 9
PC . PC(
PC), TV UI .
PC
. PC (WebKit)
.
3 .
.
.
1-6
1-6-1
DOM .
DOM 3
.
DOM .
DOM . DOM
.
. , .
, .
1.1 .
10 l
DOM+
1.1
1-6-2
1.1 .
. ,
. ,
. . ,
( ) .
.
.
.
.
window .
1 l 11
1-7
1-7-1
2005 .
prototype.js. prototype.
js . prototype.js
Ajax .
2000 Ajax Ajax
. API
. prototype.js API API . DOM
API, API API .
1.4 .
1.4
URL
prototype.js http://www.prototypejs.org/
script.aculo.us http://script.aculo.us/
jQuery http://jquery.com/
Ext.js http://www.sencha.com/products/extjs/
Yahoo! UI LibraryYUI http://developer.yahoo.com/yui/
Dojo http://dojotoolkit.org/
MochiKit http://mochi.github.com/mochikit/
MooTools http://mootools.net/
Google Closure Library http://code.google.com/closure/library/
uupaa.js http://code.google.com/p/uupaa-js/
12 l
1-7-2
. .
y
y ( )
y ( )
1.5 .
1.5
URL
Google Closure Compiler http://code.google.com/closure/compiler/
YUI Compressor http://developer.yahoo.com/yui/compressor/
packer http://dean.edwards.name/packer/
JSMin http://www.crockford.com/javascript/jsmin.html
, , .
.
. .
, ,
.
.
, .
. .
1 l 13
1-7-3
(IDE,
Integrated Development Environment) . 1.6 IDE
.
1.6 IDE
Orion IDE
Cloud9 http://cloud9ide.com/ IDE
Eclipse , IDE
NetBeans ( . IDE
Aptana Studio AppceleratorTitanium Aptanahttp://www.aptana.org/
WebStorm JetBrains http://www.jetbrains.com/webstorm/
Komodo IDE ActiveState http://www.activestate.com/komodo-ide
Perf
ect
Java
Scri
pt
2
2 .
.
17
02
2-1
.
y
y C
y
y
y
y
.
,
.
18 l
.
,
.
.
.
.
.
.
. ,
JIT(Just In Time)
.
.
, .
C
C . if while
.
.
.
C
.
.
.
.
2 l 19
.
.
.
.
.
. .
.
.
.
, .
(
) . .
.
.
2-2
2 . smjs(SpiderMonkey )
. ECMAScript 5 1.8.5 ,
.
20 l
.
js> var s = 'foobar'; //
( )
2
.
smjs typein: .
. (typein:1:
) .
js> x;
typein:1: ReferenceError: x is not defined
2-2-1 print
print 2 print
. alert document.write
. (FireBug) Node.js( 6 ) console.log
.
//
var print = alert;
var print = document.write;
// Node.js
var print = console.log;
2 l 21
2-3
2-3-1
2 .
. . 2
.
var .
js> var foo; // foo
.
.
(=) . , .
js> foo = "abc"; // foo "abc"
.
.
js> var foo = "abc"; // "abc" foo
( ).
. , .
.
js> var foo;
js> foo = "abc"; // foo "abc"
js> foo = 123; // foo 123
22 l
.
js> var n = 7; // n 7
js> n + 1;
8
.
= .
.
.
.
.
undefined (undefined
).
. undefined
.
js> var foo;
js> print(foo); // foo undefined
undefined
( ) ReferenceError .
, .
2-3-2 var .
js> print(x);
ReferenceError: x is not defined
2-3-2 var
var .
var var .
.
2 l 23
. .
var , .
var .
. .
var .
. . var . ECMAScript
5 strict mode .
2-3-3
ECMAScript .
. .
(SpiderMonkey) .
var const .
. const
.
js> const FOO = 7; //
js> print(FOO);
7
.
.
js> const FOO = 7;
js> FOO = 8; //
js> print(FOO); //
7
undefined.
. const .
24 l
js> const FOO;
js> print(FOO);
undefined
2-4
2-4-1 ?
. ,
,
C PHP .
. 6
.
2 .
. ( )
. , .
.
. .
( 2.1).
2.1
print('1');
function f() { //
print('2');
}
print('3');
f(); //
2 l 25
// 2.1
1
3
2
2-4-2
. function ,
, .
//
function (, , ) {
}
2.2 . sum, a b.
. 2.2 sum
return .
2.2 sum
function sum(a, b) {
return Number(a) + Number(b);
}
sum .
. 3 4 sum .
// sum
js> sum(3, 4);
7
26 l
. 1
.
.
.
. ( )
.
2-4-3
. function
, , . .
//
function (, , ) {
}
function (, , ) {
}
.
.
. .
( 5 ).
.
.
.
( ).
.
(Lisp) ,
1 .
2 l 27
.
2.3 . .
2.3
var sum2 = function(a, b) {
return Number(a) + Number(b);
}
var sum2 . function
. 2.3 sum2 .
sum2 .
// sum2
js> sum2(3, 4);
7
2.2 sum . 2.2
2.3 .
( ) . 2.2 2.3
. 6
.
.
. sum
.
. .
// 2.2
js> var sum3 = sum;
// sum3
js> sum3(3, 4);
7
28 l
2-4-4
C
. ,
. . .
.
. .
.
, .
2-5
2-5-1 ?
.
, .
.
.
( ).
. .
.
(java.util.Map)
. / .
.
.
2 l 29
. 5
.
. 2
2-5-2
. {}
.
//
{ : , : , }
, , .
. 2.4 .
2.4
{ x:2, y:1 } //
{ "x":2, "y":1 } //
{ 'x':2, 'y':1 } //
{ 1:2, 2:1 } //
{ x:2, y:1, enable:true, color:{r:255, g:255, b:255} } //
ECMAScript 5 .
ECMAScript 3 .
.
{ x:2, y:1, } // ECMAScript 5 .
2 5 .
30 l
.
( 5 ).
//
js> var obj = { x:3, y:4 }; // obj
js> typeof obj; // typeof object
object
obj obj . 5-2-3
.
2-5-3
(.) .
.
js> print(obj.x); // obj x
3
.
js> var obj2 = { pos: { x:3, y:4 } };
js> print(obj2.pos.x);
3
.
js> obj.x = 33; //
js> print(obj.x);
33
.
2 l 31
js> obj.z = 5; //
js> print(obj.z);
5
2-5-4 ()
([]) . []
.
js> print(obj['x']); // obj.x
3
[] .
js> var name = 'x';
js> print(obj[name]); // obj.x
3
.
js> obj['z'] = 5; // z 5 ( z )
5 .
2-5-5
, .
.
js> obj.fn = function (a, b) { return Number(a) + Number(b); };
// obj fn
.
js> obj.fn(3, 4); //
7
32 l
2.2
.
js> obj.fn2 = sum; // sum 2.2
js> obj.fn2(3, 4); //
7
. .
. 3
2-5-6 new
new . .
// new
js> var obj = new Object();
js> typeof obj; // typeof object
object
new
.
new .
, new . new
.
2-5-7
.
.
.
3 5 .
2 l 33
.
new ()
. , new Object() Object
.
2-5-8
(Object String )
. 2.1 .
2.1
-
static static
prototype
2.1 5
.
String String
. String.fromCharCode(0x41) .
, C++ .
prototype
. String String ( ) str
str.trim() str.length .
prototype
. , String trim String.prototype .
.
34 l
2-5-9
, .
.
. .
( )
. ,
. ( )
.
2-6
.
. Array Array
. , .
, .
.
//
js> var arr = [1, 100, 7];
. . 0
. ( 1)
.
2 l 35
//
js> print(arr[1]); // 1
100
js> arr[1] = 200; // 1
js> print(arr[1]);
200
[] .
//
js> var n = 1;
js> print(arr[n]); // a[1]
200
js> print(arr[n + 1]); // a[2]
7
. .
.
//
js> var arr = [1, 'foo', 7];
36 l
.
.
.
.
.
, ,
.
.
. .
.
.
.
, .
. HTML
.
.
. PHP
.
.
C O L U M N
37
03
3-1 ?
.
.
.
y
y
y
y null
y undefined
.
.
.
38 l
.
6 .
. .
, .
.
.
.
3-1-1
.
. ,
.
vs.
. .
.
. ,
. , ,
.
,
. 1
vs.
(int double) ,
.
1 . , .
3 l 39
. ,
. () .
. .
, .
, . ,
. .
. .
3-1-2
. ,
. .
.
5 .
3-2
ECMAScript (built-in type)
. (primitive type) , (primitive value)
.
.
3-2-1
.
.
. .
.
(+ ) .
40 l
.
.
. 1 .
, 64 .
double .
.
. ,
. 2
. true false
.
null null . null .
null null .
null null . ,
.
undefined .
undefined
.
3-3
3-3-1
. (")
(') . .
js> var s = "abc"; // s
js> print(s); // s
abc
2 .
3 l 41
js> var s = 'abc'; // s
js> print(s); // s
abc
.
.
(\). , \n .
3.1
\n (LF)
\t
\b
\r (CR)
\f
\v
\\
\'
\"
\xXX Latin-1 (X 0 9 a f )
\uXXX Unicode (X 0 9 a f )
. ,
.
js> var s = 'I say "yes"'; // " ( )
js> print(s);
I say "yes"
.
42 l
3-3-2
= .
.
.
js> var s = 'abc'; // 'abc' s
js> var s2 = s; // s s2
js> print(s2); // s2 'abc'
abc
, s
s2 .
.
.
. +
. .
js> var s1 = '012';
js> var s2 = '345';
js> var s3 = s1 + s2; //
js> print(s3); // s3 '012345'
012345
+= .
js> var s = '012';
js> s += '345';
js> print(s); // s '012345'
012345
012 345 012345
. s2 012 .
3 l 43
js> var s = '012';
js> var s2 = s;
js> s += '345'; // s '012345'
js> print(s2); // s2 '012'
012
typeof . typeof string
. .
js> typeof 'abc'; // typeof
string
js> var s = 'abc';
js> typeof s; // s typeof
string
js> typeof(s); //
string
js> typeof(typeof(s)); // typeof
string
3-3-3
. === ==.
!== != .
=== == . ===
. ECMAScript (strict equal) .
, .
=== == .
. .
js> var s1 = '012';
js> var s2 = '0';
js> var s3 = s2 + '12';
js> s1 == s3; //
true
44 l
js> s1 === s3; //
true
js> s1 != s3;
false
js> s1 !== s3;
false
. > , >= , <
, var s1 = 'abc';
js> var s2 = 'def';
js> s1 < s2;
true
js> s1 s1 > s2;
false
js> s1 >= s2;
false
.
.
y (ABC)
y
y ( )
3 l 45
3-3-4 (String )
.
.
String. String
(Number Integer )
. .
String
. . ,
.
js> var s = '012';
js> s.length; //
3
js> '012'.length; //
3
String 3 .
( "012".length() )
. String
.
, '012'.length
, length .
.
.
3-3-5
new . new 5
.
3 String String .
46 l
var sobj = new String('abc'); //
new .
. . sobj
+ .
js> var s = sobj + 'def'; //
js> print(s);
abcdef
.
.
. , .
. .
js> var sobj1 = new String('abc');
js> var sobj2 = new String('abc');
js> sobj1 == sobj2; // false
false
js> sobj1 === sobj2; // false
false
+
.
// ( )
js> sobj1 + '' == sobj2 + '';
true
js> sobj1 + '' === sobj2 + '';
true
==
.
3 l 47
js> var sobj = new String('abc');
js> var s = 'abc';
js> sobj == s; // true
true
js> sobj === s; // false
false
.
3-3-6
typeof .
typeof object.
js> var sobj = new String('abc');
js> typeof sobj;
object
. new String()
. , .
,
String .
.
. ,
.
.
3-3-8 String .
js> var s = 'abc'; // 1
js> s.charAt(1);
b
js> 'abc'.charAt(1); //
b
48 l
3-3-7 String
new ( ) String
. String
.
js> var s = String('abc');
js> typeof s; // s
string
js> var s = String(47); //
js> print(s);
47
js> typeof s; // s
string
3-3-8 String
String 3.2 .
3.2 String
String([value]) value
new String([value]) String
String 3.3 . String.fromCharCode(0x41)
.
3.3 String
fromCharCode([char0[, char1, ...]) value
length 1
prototype
String.prototype 3.4 .
3 l 49
3.4 String.prototype
charAt(pos) pos 1 . 0 , pos .
charCodeAt(pos) pos . pos NaN .
concat([string0, string1, ...]) .
constructor String
indexOf(searchString[, pos]) searchString .
pos . -1
localeCompare(that) . , 0, .
match(regexp) regexp .
quote() . .
replace(searchValue, replaceValue) searchValue( ) replaceValue( )
search(regexp) regexp
slice(start, end) start end . start end .
split(separator, limit) separator , .
substr(start[, length]) . start length . start .
substring(start, end) start end . slice .
toLocaleLowerCase()
toLocaleUpperCase()
toLowerCase()
toSource() . String .
toString() String
toUpperCase()
trim()
trimLeft() . ()
50 l
trimRight() . ()
valueOf() String
String 3.5 . String str.
length .
3.5 String
( )
length ()
. String .
js> var s = new String('abc');
js> print(s[1]); // 1
b
js> print('abc'[2]); //
c
3-3-9
. , .
3.4
.
js> var s = new String('abc');
js> var s2 = s.toUpperCase(); // s toUpperCase
js> print(s, s2); // s
abc ABC
js> s[0] = 'A'; // []
js> print(s);
abc
3 l 51
. String
Array .
.
. 5-12 .
3-4
3-4-1
64 .
.
.
.
. ,
.
3.6 .
3.6
0
51
-7 ( )
0x1f 16(a f . 0x 0X )
3.14
.14 . 0.14
3e2 3 10 2. , 300. e E
3.14e2 3.14 10 2. , 314
3.14e-2 3.14 10 -2. , 0.0314
.
52 l
js> var n1 = 1; // n1 1
js> var n2 = 2; // n2 2
js> n1 + n2; // n1 n2
3
8 . , ECMAScript
. .
typeof . typeof
number .
js> var n = 1;
js> typeof n; // typeof
number
js> typeof 1; // typeof
number
3-4-2
4 +, -, *, / . %
( ) . 4
, , 4.5 .
. , 0
.
Math . PI e
. , 2 10 Math.
pow . 5-21-2 Math .
3 l 53
3-4-3
.
double float .
.
.
.
.
.
.
js> 0.1 + 0.2; // 0.1 0.2 0.3
0.30000000000000004
js> (0.1 + 0.2) == 0.3; //
false
js> (0.1 + 0.2) === 0.3; //
false
js> 1/3; // 1 3
0.3333333333333333
js> 10/3 - 3; //
0.3333333333333335
js> (10/3 - 3) == (1/3); //
false
js> (10/3 - 3) === (1/3);
false
.
. 53
. 53 .
BigDecimal
. . JVM
(Rhino) BigDecimal
.
54 l
3-4-4 (Number )
(String ) (Number ) .
.
, .
.
js> (1).toString(); //
1
js> typeof (1).toString(); //
string
new .
.
js> var nobj = new Number(1);
js> var nobj1 = new Number(1);
js> nobj == nobj1; // false
false
js> nobj === nobj1; // false
false
js> nobj == 1; // true
true
js> nobj === 1; // false
false
.
typeof . typeof object.
js> var nobj = new Number(1);
js> typeof nobj;
object
3 l 55
3-4-5 Number
String Number .
.
js> var n1 = Number(1);
js> typeof n1; // n1
number
js> n1 == 1;
true
js> n1 === 1;
true
js> var n = Number('1'); //
js> print(n);
1
Number NaN . new
( ) . 4
js> var n = Number('x');
js> print(n);
NaN
js> typeof n;
number
js> var nobj = new Number('x');
js> print(nobj);
NaN
js> typeof nobj;
object
3-4-6 Number
Number 3.7 .
4 NaN .
56 l
3.7 Number
Number([value]) value
new Number([value]) Number
Number 3.8 . Number.NaN .
3.8 Number
prototype
length 1
MAX_VALUE 64
MIN_VALUE 64
NaN Not a Number
NEGATIVE_INFINITY
POSITIVE_INFINITY
3.9 Number.prototype .
3.9 Number.prototype
constructor Number
toExponential(fractionDigits) . fractionDigits
toFixed(fractionDigits) . fractionDigits
toLocaleString()
toPrecision(precision) . precision
toSource() . Number
toString([radix]) Number . radix
valueOf() Number
3.10 Number .
3 l 57
3.10 Number
( )
3-4-7
64 Number
. () ,
. 5
js> Number.MAX_VALUE;
1.7976931348623157e+308
js> Number.MIN_VALUE;
5e-324
js> -Number.MAX_VALUE;
-1.7976931348623157e+308
js> -Number.MIN_VALUE;
-5e-324
Number.MAX_VALUE.toString(16)
16 . .
IEEE754 .
IEEE754 Number
. 3.11 .
3.11
Number.POSITIVE_INFINITY
Number.NEGATIVE_INFINITY
Number.NaN Not a Number
5 +0 -0 ( ).
58 l
.
js> Number.POSITIVE_INFINITY;
Infinity
js> Number.NEGATIVE_INFINITY;
-Infinity
js> Number.NaN;
NaN
( NaN) IEEE754
. (typeof number )
. , 2
2 .
.
js> var inf = Number.MAX_VALUE * 2;
js> print(inf); // 2
Infinity
js> inf / 2; // 2
Infinity
js> inf * 0; // 0 0
NaN
NaN 3-4-8 NaN .
3-4-8 NaN
NaN NaN .
NaN NaN.
3 l 59
js> NaN + 1;
NaN
js> NaN * 0;
NaN
js> NaN - NaN;
NaN
NaN .
NaN false .
js> NaN == 1; // false
false
js> NaN === 1; // false
false
js> NaN == NaN; // false
false
js> NaN === NaN; // false
false
NaN false.
js> NaN > 1
false
js> NaN >= 1
false
js> NaN > NaN
false
js> NaN >= NaN
false
NaN isNaN
. isNaN NaN NaN true
.
js> isNaN(NaN);
true
60 l
js> var nanobj = new Number(NaN); // NaN Number
js> typeof nanobj;
object
js> isNaN(nanobj); // true
true
js> isNaN({}); // NaN
true
isFinite (NaN )
.
js> isFinite(1); // true
true
js> isFinite(NaN);
false
js> isFinite(Infinity);
false
js> isFinite(-Infinity);
false
( 3.12). Infinity NaN
.
.
3.12
x y x/y x%y
0.0 NaN
0.0 x
0.0 0.0 NaN NaN
NaN
NaN NaN
Infinity NaN .
( ). 3.12
Infinity NaN .
3 l 61
// ECMAScript 5
js> NaN = 7;
js> print(NaN);
NaN
js> Infinity = 8;
js> print(Infinity);
Infinity
ECMAScript 5 .
.
3-5
3-5-1
.
true false . .
.
js> var flag = true;
js> print(flag);
true
js> print(!flag); // true false
false
js> print(!!flag); //
true
true false .
. , var flag=true true
var n=0 0 .
typeof boolean.
62 l
js> typeof true;
boolean
js> typeof false;
boolean
4 , , .
3-5-2 (Boolean )
(Boolean ) .
String Number .
. .
js> true.toString(); // Boolean
true
String Number new .
, String Number .
js> var t = new Boolean(true); //
js> t;
true
js> typeof t; //
object
js> t == true; // true
true
js> t === true; // false
false
Boolean .
.
js> var tval = Boolean(true); //
js> typeof tval;
3 l 63
boole