Cet<pemb1 JauaScript HU_ g3SI Bmopoe u3gaHue il>HOH Pe3uz 5e3p 5ufio t1ocun Mapac CeKpembt JavaScript HUHa3R BTopoe M3,UaHMe Secrets of the JavaScript Ninja Second Edition JOHN RESIG, BEAR BIBEAULT, JOSIC MARAS II MANNING Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 CeKpembt JavaScript HUHa3R BTopoe M3,UaHMe EEK 32.973.26-018.2.75 P34 Y)J,K 681.3.07 KoMnhIOTepHoe H.3,n:aTeJihCTBO ",[t:ttaJieKTH.Ka" 3asp.e .n:aKuttC.dH.i Tpury6 ITepesoa.n:t tcrn tti1cKHo preo.n: aKUHHH.B . EepUJ,meiiilla Ilo 06II.1sHoMrr pocao6Mp amaihesc 11b3 .n;areJib"C.l1:T11Ba;rOe KT11Krrao"a .n;pecy: [email protected]:o/m ,/. wwdwialektika.com Pe3Hr, )],)KOH, BH6o, fie::ipM, apac, I1octtn. P34 CeKpeTbI JavaScript HHH,[(3.H, 2-e H.3,a;. : ITepc. aHrn.- Cnfi. 000 "Albl cpa- KHttra", 2017. -544 c.:HJI . -ITapaTJIH.T a.H rn. ISBN 978-5-9908911-8-0 (pyc.) BBK 32.973.26-018.2.75 BeeH a3BaHHrrllp orpaMMHbrIrXp o.n;yKTl!OBBJ il!IOJTaCplle r11CTp11posarHoHpbrIoMB1b1I MMaHp KaM11 COOTBCTCTBYIqOmIpIM.\.1 1X H�IKaK'all!a CHTabC TOllll.ler1o13 ,l\aHHHl1ll B KaKHXL\ eJil!HXe M O)l(6ebT! Tsbo crrpo113sBe K1.1aeKHOaH 6bIT OH H 6bIIOJ qiopMe1 1K aK11M6l1b IT OH l16 bIIOJ cpe1.1crnaM61y1l.l,bT O3 JieKTPOHH1b1iJMeIC HX aHH'le CKHeB,K JIIO<P'olTaOl!K OIIHpoBaHH 3HaeI IHHCabM arHHTHbHIOHC HTCJe!CbJ,I HHa 3 TOH eTI IHCbMeHHOfO pa3peweHHHllJ ,l\aTeJibMCaTnBnai nPgu blicatCioo.n , Authorizterda ntsiloafnr omt heE nglislha nguaegdei tipounb lishbeydM anningP ublicatiCoo,n s Copyrig©h t2016. Allr ighrtess erved. No parotf t hipsu blicatmiaoynb er eproducesdt,o reidna retriesvyaslt eomr,t rnasmitteidna, n y formo rb ym eanse lecotnricm,e chanicaplh,o tocoipnygo,r o thenviwsiet,h ouptr iowrr ittpeenn nis sioonf t hep ublisher. Russialna nguaegdei tiiosnp ublishbeydW illiaPmusb lishiHnogu sea ccorditnogt heA greement withR& IE nterpriIsnetse rnatiCoonpaylr,i g©h t2017 Ha ywHo-nonyMpuoe u3aauue n ,ll;)IWH Pe3Hr, Be3p nH6o, HocH Mapac CeKpeThI JavaScript HHH�3.H 2-e 113.z:i;amie Jl11TeparypHpbeI.n:Ha KToHp. A. IIonoea BepcTKMa .A. YOaJWB Xy_a;o)l{eCTBCpHeH.n:balKHT oEp.JI. ,l{ot1tnuK KoppeKToJIp. A. I'ojJOU£1lKO I101.1rrncBa nHeo' la0T4b.0 9.2017. <I>opMaT7 0xl00/16. fapHHTypTai mesI.l e'laoT<Pbc eTHall. Yrn. rre'-1.J I4.3, 86. Y'-l.-H3/.I. Jl. 28.36. T11pa4)0I0 (3 K33.a KaN32 6055. 0Tne'laTaaHA oO «Ileps0a6llp a3L\OBanam orpaqinll» <l>HJIH«al JlIex oBCKIlHel'-lla THbI.[(Hs op» 142300, MocKOBCKaol!6 ;racrT. lbl,e xoys;r,. Il oJI11rpaqi11.n;.c 1T OB, 000 "AJib<Pa-KHHr19a5"0,27 , CaHKT-IleTeypp6rM,a rmIToropcKyMJ I1-1.. 3,0 ISBN 978-5-9908911-8-0 (pyc.) © KoMIIbIOTep1H1o3e/. laTeJ"i.l(bHCaTJBiOe KTH2K0a17" , rrepeao1.1o,q iopMJieHMHaeK,e T11posaHne ISBN 978-1617-29285-9 (a1-ir.JI.) ©by ManninPg ublicatCioon.2s,0 16 OrnaeneHMe "lacTb Pa3MMHKa 27 I. rnaea JavaScript noect0AY 29 1. rnaea Co3AaHr.te crpaHr.tL\bl e Ar.tHaMr.t'fecKoM pe>Kr.tMe 41 2. "laCTbI I. npeACTaBneHMe 0 <l>YHKU,MSIX 61 r11aea d>yHKL\r.tr.t BblCWero nOPHAKa AllH Ha'fr.tHat0114r.tx: 3. onpeAeneHr.tH r.t apryMeHTbl 95 r11aea d>yHKL\MM AllH y'feHr.tKa Macrepa: npeACTaBlleHr.te 06 MX Bbl30Be 87 4. r11aea d>yHKL\Mr.t AllH MaCTepa: 3aMblKaHMH M 0611aCTM BMAMMOCTM 129 5. r11aea 6. d>yHKL\Mr.t Ha nepcneKrr.tey: reHeparopb1 r.t 06e114aHr.tS1 171 "laCTbI ll. "1ccneAOB3HMe o6beKTOB M ynpolfeHMe KOA3 215 rnaea 06'beKTHaSI op11e1 HTaL111tH c nOM01!4blO npOTOTMnOB 217 7. r11aea Ynpae11eHr.te AOCTynoM K o6'beKTaM 253 B. r11aea Pa6ora c KOlllleKL\MHMM 283 9. r11aea OenaAeHr.te pery11HpHblMM Bblpa>KeHMHMM 323 10. r11aea MeTOAMKlll MOAYl1Slpr.t3aL\MM KOAa 351 11. "lacTb MccneAOBaHMe 6pay3epoB 375 IV. rnaea Pa6ora c MOAellbtO DOM 377 12. r11aea Oco6eHHOCTlll o6pa60TKr.t C06blTMM 409 13. rnaea Crparernr.t pa3pa60TKM Kpocc-6pay3epHoro KOAa 451 14. "lacTb Op1rtno>KeHMS1 475 V. npr.t11o>KeH11te AonOllHMTellbHble cpeACTBa CTaHAapra ES6 477 A. np11111o>KeH111e CpeAcrea recr111poeaHr.tH 111 oTllaAKr.t 483 6. npr.tllO>KeHr.te OreeTbl Ha ynpa>KHeHMH 505 B. npeAMeTHblM yKa3aT611b 533 0T3bIBbl 0 rrepBOM l13,[(aHHH 13 Or asTOpa 14 6narOAapHOCTM 16 06 3TOH KHMre 18 KoMy a.z:i;pecoBatta KHHra 18 CTpyKrypa KHHrH 18 YcJIOBHhie o6o3Ha'-leHHJI, rrpHHJ!Thie B KHHre 21 3arpy)l(aeMblH HCXO,[(HbIH KO,[( 21 KaK CBJI3aThCJI c aBTopaMH 21 06 asTOpax 22 06 Mnmocrpa�MM Ha o6no>KKe KHMrn 24 OT H3,z:i;aTeJibCTBa 25 �acrbl. PaaMMHKa 27 rnasa 1. JavaScript noBCIOAY 29 1.1. 06rn:ee rrpe,z:i;CTaBJieHHe o JI3bIKe JavaScript 30 1.1.1. .l1:aJihtteH:rnee pa3BHTHe JavaScript 32 1.1.2. TpattcrrHJIJITopbI, 06ecrre'-IHBa10rn;He .z:i;ocryrr K 6y.z:i;yrn:eMY JavaScript cero,z:i;HJI 33 1.2. 06rn:ee rrpe,z:i;CTaBJiem1e o 6paY3epe 33 1.3. HopMbI rrepe,z:i;oBoH: rrpaKTHKH 35 1.3.1. Orna,z:i;Ka 36 1.3.2. TeCTHpoBaHHe 36 1.3.3. AttaJIH3 rrpoH3BO,[(HTeJibHOCTH 37 1.4. y CHJieHHe rrepeHOCHMOCTH rrpHo6peTeHHbIX HaBhIKOB 38 39 Pe3IOMe rnasa C03AaHMe CTpaHM�bl B AMHaMM"leCKOM pe>KMMe 41 2. 2.1. 06rn:ee rrpe,z:i;cTaBJieHHe 0 )l(H3HeHHOM QHKJie Be6-rrpHJIO)l(eHHJ! 42 2.2. CTa,z:i;HJI co3,z:i;aHHJ1 CTpaHHQhI 45 2.2.1. CHHTaKCH'-leCKHH attaJIH3 Ko,z:i;a HTML rroCTpoeHHe Mo,z:i;eJIH DOM 46 II 2.2.2. Bh1rrOJIHeHHe Ko,z:i;aJavaScript 48 2.3. 06pa6oTKa co6hITHH 52 2.3.1. 06rn:ee rrpe,z:i;cTaBJieHHe 06 o6pa6oTKe co6bITHi1: 53 Pe3IOMe 59 Yrrp(l)Kttemrn: 59 LfaCTb npeACTaeneHHe 0 <t>YHK�l'ISIX 61 II. rnasa <l>yHK�llUt BblCWero nopSIAKa AnSI H3"tl/IH3IO�llXl : 3. onpeAeneH111S1 111 apryMeHTbl 63 3.1. fJiaBHOe OTJIIfqlfeJ avaScript KaK 513bIKa cpyHKl_J;HOHaJibHOro rrporpaMMHpOBaHH51 64 3.1.1. <I>yHKIJ;HH B Ka qecTBe o6beKTOB BbICUiero rrop51,[1Ka 65 3.1.2. <I>yHKIJ;HH o6paTHOro Bbl30Ba 66 3.2. Oco6eHHOCTH rrpHMeHeHH51 cpyttKQHH B KaqecTBe o6beKTOB 70 3.2.1. CoxpatteHHe cpyttKQHH 71 3.2.2. CaM03arroMHHaIOIQHern cpyttKQHH 73 3.3. Orrpe,[leJieHHe cpyttKQHH 75 3.3.1. 06b51BJieHH51 cpyHKIJ;HH If cpyHKIJ;HOHaJibHbie Bblp(l)KeHH51 77 3.3.2. CTpeJioqHhre cpyttKQHH 82 3.4. ApryMeHThI H rrapaMeTpbr cpyttKQHH 84 3.4.1. OcTaBUIHern rrapaMeTpbr 86 3.4.2. CTaH,[lapTHhre rrapaMeTpbr 88 Pe3IOMe 91 Yrrp(l)l(HeHH51 92 rnasa <l>yHK�111111 Ansi y1.teH11K1 a Mac1epa: npeAcTasneH11e1 0611x1 Bb1aose 95 4. 4.1. 11crrOJib30BaHHe He51BHbIX rrapaMeTpOB cpyHKIJ;HH 96 4.1.1. ITapaMeTp arguments 96 4.1.2. ITapaMeTp this, rrpeL\CTaBJI51IOIQHH KOHTeKCT cpyttKQHH 101 4.2. BbI30B cpyHKIJ;HH 102 4.2.1. Bhr30B KaK cpyttKQHH 103 4.2.2. Bbr30B KaK MeTo,[la 104 4.2.3. Bhr30B KaK KOHCTPYKTopa 107 4.2.4. Bbr30B qepe3 MeTOL\hI apply () H call () 113 4.3. Pa3peUieHHe 3aTPYLIHeHHH, CB513aHHhIX c KOHTeKcTaMH cpyttKQHH 120 4.3.1. 06paIIJ;eHHe c KOHTeKCTaMH cpyHKIJ;HH c ITOMOIIJ;bIO CTpeJioqHbIX cpyHKIJ;HH 120 4.3.2. ITpHMeHeHHe MeTO,[la bind () 12 4 Pe3IOMe 125 Yrrp(l)l(HeHH51 126 rnasa <l>yHK�llllll AnSI MaCTepa: 33MblK3HlllSI o6naCTlll BlllAllMl OCTlll 129 5. Ill 5.1. 06IQee rrpe,[(CTaBJieHHe 0 3aMbIKaHH51X 130 5.2. ITpHMeHeHHe 3aMbIKaHHH Ha rrpaKTHKe 134 5.2.1. 11MHTaI_J;H51 3aKpbITbIX rrepeMeHHbIX 134 5.2.2. ITpHMeHeHHe 3aMhIKaHHH rrpH o6paTHbIX BhI30Bax 136 8 CoaepJ1Cauue 5.3. 0TcJie:11<IrnaHHe BbIIIOJIHeHHR KO)J;a c IlOMOW:bIO KOHTeKCTOB BbIIIOJIHeHHR 139 5.4. 0TCJie)KIIBaHHe H,JJ;eHTHCpHKaTopoB c IIOMOIII:blO JieKcnqecKHX cpe;:i; 142 5.4.1. BJio)KeHHe Ko;:i;a 143 5.4.2. BJIO)KeHHe Ko;:i;a H JieKcnqecirne cpe;:i;hr 144 5.5. 06w;ee npe;:i;cTaBJieHHe o THIIax nepeMeHHbIX BJavaScript 147 5.5.1. I13MeHReMOCTb nepeMeHHbIX 14 7 5.5.2. KJi10qeBbie CJIOBa )J;JIR onpe;:i;eJieHHR nepeMeHHbIX H JieKcnqecKne cpe;:i;hI 150 5.5.3. PerncTpau;HR n;:i;eHTH¢HKaTopoB B JieKcw1ecKHX cpe;:i;ax 154 5.6. I1ccJie,JJ;OBaHHe npHHJJ;HIIa ;:i;e:i1:CTBHR 3aMbIKaHHH 159 5.6.1. Ew;e pa3 06 HMHTaJJ;HH 3aKpbITbIX nepeMeHHbIX c noMOID:bIO 3aMbIKaHHH 159 5.6.2. Pa3bRCHeHHe no noBo;:i;y 3aKpbITbIX nepeMeHHbIX 164 5.6.3. Ew;e pa3 o 3aMbIKaHHRX H o6paTHbIX BbI30Bax 165 Pe3IOMe 167 Ynpa)l(HeHHR 168 rnaea 6. <l>yHK�i.w1 Ha nepcneKT1-1ey: reHepaTOpb1 1-1 06e11.4aH1-1si 171 6.1. 2J:ocTH)KeHHe H3RID:HOCTH aCHHXpOHHOro KO)J;a c IIOMOID:blO reHepaTopoB H o6em:aHHH 172 6.2. I1cIIOJlb30BaHHe <i>YHKJJ;HH-reHepaTopoB 174 6.2.1. YnpaBJieHHe reHepaTopoM c noMOIII:bIO HTepaTopa 176 6.2.2. IIpnMeHeHHe reHepaTopoB 179 6.2.3. 06MeH ;:i;aHHbIMH c reHepaTopoM 183 6.2.4. I1ccJie;:i;oBaHHe BttyTpeHHero MexaHH3Ma ;:i;e:i1:cTBHR reHepaTopoB 186 6.3. Pa6oTa c o6em:aHHRMH 193 6.3.1. 3aTpy;:i;HeHHR, CBR3aHHbie c npocTbIMH o6paTHbIMH BbI30BaMH 195 6.3.2. Yrny6JieHHOe nccJie;:i;oBaHne o6ern;aHHH 197 6.3.3. 0TKJIOHeHHe o6em:aHHH 200 6.3.4. Co3;:i;aHne nepBoro HacToRm:ero o6em:aHHR 202 6.3.5. CBR3bIBaHHe o6em:aHH:i1: B u;enoqKy 204 6.3.6. O)KH,JJ;aHne pR;:i;a o6ern;aHHH 205 6.4. CoqeTaHHe reHepaTopoB H o6em:aHH:i1: 207 6.4.1. AcnHxpoHHbie ¢YHKJJ;HH B nepcneKTHBe 211 Pe310Me 212 Ynpa)l(HeHHR 213 'iaCTb MccneAOBaHHe o6beKTOB H ynpo1.1eHHe KOAa 215 Ill. rnaea 06beKTHasi op1-1eHTa�1-1si c noM01.J.4blO npo1ornnoe 217 7. 7.1. 06m:ee npe;:i;CTaBJieHHe o npoTOTHnax 218 7.2. Co3;:i;aHHe o6oeKTOB H npoTOTHIIbI 221 7.2.1. CBoilcTBa 3K3eMIIJIRpoB 224 7.2.2. IIo6oqHbie a¢¢eKTbI ;:J;HHaMnqernoro xapaKTepaJavaScript 227 7.2.3. T11mrnaQ115I o6beKTOB qepe3 KOHCTPYKTopbI 230 7.3. ,ll,OCTH)KeHIIe HaCJ1e,101; BaHirn 232 7.3.1. Tpy,11;HocT11 rrepeorrpe,11;eJieH115I rnoi1cTBa constructor 236 7.3.2. OrrepaQHR instanceof 240 7.4. I1p11MeHeH11e "KJiaccoB" B cTaH,11;apTe ES6 513hIKajavaScript 242 7.4.1. I1p11MeHeH11e KJiroqeBoro CJIOBa class 243 7.4.2. PeaJI113aQl151 HaCJie,101; BaH115I 246 Pe3IOMe 249 Yrrpa)l(HeH115I 250 rnaea 8. YnpaeneHille AOcrynoM K o6beKraM 253 8.1. YrrpaBJieH11e ,11;ocryrroM K CBOHCTBaM o6beKTOB c rroMOlllhIO MeTO,ll;OB IIOJIYqeHl151 11 ycTaHOBKl1 254 8.1.1. Orrpe,11;eJieH11e MeTO,ll;OB IIOJIYqeH115I 11 ycTaHOBK11 256 8.1.2. I1p11MeHeH11e MeTO,ll;OB IIOJIY'leHl15111 ycTaHOBKl1 ,lJl;l51 rrpoBepK11 ,11;ocT0BepHocT11 3Ha'leH11i1 CBOHCTB 262 8.1.3. I1p11MeHeH11e MeTO,ll;OB IIOJIY'leHl15111 ycTaHOBKl1 ,lJl;l51 orrpe,11;eJieHl15I BhI'll1CJIReMhIX CBOHCTB 264 8.2. I1p11MeHeH11e rrpoKc11-06beKTOB ,ll;Jl51 yrrpaBJieHl151 ,ll;OCTYIIOM 266 8.2.1. I1p11MeHeH11e rrpoKc11-06beKTOB ,lJl;l51 rrpOTOKOJI11pOBaHI151 270 8.2.2. I1p11MeHeH11e rrpoKc11-06beKTOB ,lJl;l51 113MepeHl151 rrp0113BO,ll1l; TeJihHOCTl1 272 8.2.3. I1p11MeHeH11e rrpoKc11-06beKTOB ,lJl;IJI aBTOMaTl1'leCKoro 3aIIOJIHeHl151 CBOHCTB 274 8.2.4. I1p11MeHeH11e rrpoKc11-06beKTOB ,lJl;IR peaJI113aQ1111 0Tp11QaTeJihHhIX 11H,ll;eKCOB MaCCl1BOB 275 8.2.5. I1po6JieMbl c rrpo113BO,ll;l1TeJibHOCTbIO rrp1111CIIOJib30BaHl111 rrpOKCl1- o6beKTOB 278 Pe3IOMe 279 Yrrpa)l(HeH115I 280 rnaea Pa6ora c KonneK1.4i'IS1Mi1 283 9. 9.1. Macc11BhI 284 9.1.1. Co3,11;aH11e MaccrrBOB 284 9.1.2. ,l0l, 6aBJieH11e 11 y,1a1; JieH11e 3JieMeHTOB c 06011x KOHQOB MaCCl1Ba 287 9.1.3. ,l0l, 6aBJieH11e 11 y,1a1; JieH11e 3JieMeHTOB B JII060M MeCTe MaccrrBa 289 9.1.4. Ha1160Jiee yrroTpe611TeJihHb1e orrepaQ1111 Ha,11; Macc11BaM11 291 9.1.5. IToBTopttoe 11crrOJih30BaH11e BCTpoeHHhIX MeTO,ll;OB 06pa60TK11 MaCCl1BOB 303 9.2. 0T06pa)KeHH51 305 9.2.1. 06beKTbl Herrp11rO,ll;Hbl B Ka'leCTBe OTo6pa)KeHl1H 306 9.2.2. Co3,11;aH11e rrepBoro 0To6pa)KeH115I 309 9.2.3. I1epe6op 3JieMeHTOB OTo6pa)KeHl1H 313