index.html 770 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>内置对象 | 彪哥博客</title>
  7. <meta name="generator" content="VuePress 1.9.5">
  8. <link rel="icon" href="/blog/img/favicon.ico">
  9. <meta name="description" content="web前端技术博客,专注web前端学习与总结。JavaScript,js,ES6,TypeScript,vue,React,python,css3,html5,Node,git,github等技术文章。">
  10. <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
  11. <meta name="theme-color" content="#11a8cd">
  12. <link rel="preload" href="/blog/assets/css/0.styles.cf8cd190.css" as="style"><link rel="preload" href="/blog/assets/js/app.28edfb28.js" as="script"><link rel="preload" href="/blog/assets/js/2.395c0d18.js" as="script"><link rel="preload" href="/blog/assets/js/3.6748bd5c.js" as="script"><link rel="preload" href="/blog/assets/js/137.71e8757e.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.cad3aa70.js"><link rel="prefetch" href="/blog/assets/js/100.08a8b2d8.js"><link rel="prefetch" href="/blog/assets/js/101.2aabb12c.js"><link rel="prefetch" href="/blog/assets/js/102.13f3cc4d.js"><link rel="prefetch" href="/blog/assets/js/103.c19aee03.js"><link rel="prefetch" href="/blog/assets/js/104.91a6aec1.js"><link rel="prefetch" href="/blog/assets/js/105.71de1aa4.js"><link rel="prefetch" href="/blog/assets/js/106.207422de.js"><link rel="prefetch" href="/blog/assets/js/107.bf754f60.js"><link rel="prefetch" href="/blog/assets/js/108.a43622c2.js"><link rel="prefetch" href="/blog/assets/js/109.3219d87b.js"><link rel="prefetch" href="/blog/assets/js/11.f2e9eca8.js"><link rel="prefetch" href="/blog/assets/js/110.f41596b0.js"><link rel="prefetch" href="/blog/assets/js/111.da99a105.js"><link rel="prefetch" href="/blog/assets/js/112.9dd75c6f.js"><link rel="prefetch" href="/blog/assets/js/113.9322f157.js"><link rel="prefetch" href="/blog/assets/js/114.c5150cc6.js"><link rel="prefetch" href="/blog/assets/js/115.f58b49f8.js"><link rel="prefetch" href="/blog/assets/js/116.8f7d9c6f.js"><link rel="prefetch" href="/blog/assets/js/117.3c970f48.js"><link rel="prefetch" href="/blog/assets/js/118.921e1d54.js"><link rel="prefetch" href="/blog/assets/js/119.0141defb.js"><link rel="prefetch" href="/blog/assets/js/12.98512c60.js"><link rel="prefetch" href="/blog/assets/js/120.de47a761.js"><link rel="prefetch" href="/blog/assets/js/121.a0b3693a.js"><link rel="prefetch" href="/blog/assets/js/122.6c7dd225.js"><link rel="prefetch" href="/blog/assets/js/123.dbff103c.js"><link rel="prefetch" href="/blog/assets/js/124.493776ef.js"><link rel="prefetch" href="/blog/assets/js/125.554c9fbf.js"><link rel="prefetch" href="/blog/assets/js/126.9d3b75dc.js"><link rel="prefetch" href="/blog/assets/js/127.aad20a7e.js"><link rel="prefetch" href="/blog/assets/js/128.6543adba.js"><link rel="prefetch" href="/blog/assets/js/129.d7c56b92.js"><link rel="prefetch" href="/blog/assets/js/13.a79fa0c7.js"><link rel="prefetch" href="/blog/assets/js/130.593d21f0.js"><link rel="prefetch" href="/blog/assets/js/131.4c90d8b8.js"><link rel="prefetch" href="/blog/assets/js/132.4ad12bdc.js"><link rel="prefetch" href="/blog/assets/js/133.6df121d8.js"><link rel="prefetch" href="/blog/assets/js/134.65e904d2.js"><link rel="prefetch" href="/blog/assets/js/135.47498729.js"><link rel="prefetch" href="/blog/assets/js/136.d99350df.js"><link rel="prefetch" href="/blog/assets/js/138.75e29c6f.js"><link rel="prefetch" href="/blog/assets/js/139.766f20b7.js"><link rel="prefetch" href="/blog/assets/js/14.0fdf0c78.js"><link rel="prefetch" href="/blog/assets/js/140.0d9b8fbc.js"><link rel="prefetch" href="/blog/assets/js/141.128b6e26.js"><link rel="prefetch" href="/blog/assets/js/142.e004a584.js"><link rel="prefetch" href="/blog/assets/js/143.5a19a0b2.js"><link rel="prefetch" href="/blog/assets/js/144.5f397211.js"><link rel="prefetch" href="/blog/assets/js/145.3b994de3.js"><link rel="prefetch" href="/blog/assets/js/146.24441c89.js"><link rel="prefetch" href="/blog/assets/js/147.f7f1952e.js"><link rel="prefetch" href="/blog/assets/js/148.25581f95.js"><link rel="prefetch" href="/blog/assets/js/149.7a6ca4f7.js"><link rel="prefetch" href="/blog/assets/js/15.fce722b2.js"><link rel="prefetch" href="/blog/assets/js/150.3a1675b7.js"><link rel="prefetch" href="/blog/assets/js/151.250a4894.js"><link rel="prefetch" href="/blog/assets/js/152.fa700729.js"><link rel="prefetch" href="/blog/assets/js/153.cc421f44.js"><link rel="prefetch" href="/blog/assets/js/154.a04c0164.js"><link rel="prefetch" href="/blog/assets/js/155.b5c0abcd.js"><link rel="prefetch" href="/blog/assets/js/156.2ba750fc.js"><link rel="prefetch" href="/blog/assets/js/157.99ee745c.js"><link rel="prefetch" href="/blog/assets/js/158.09b15f2b.js"><link rel="prefetch" href="/blog/assets/js/159.3fd855e1.js"><link rel="prefetch" href="/blog/assets/js/16.7cf1b239.js"><link rel="prefetch" href="/blog/assets/js/160.307b6a2e.js"><link rel="prefetch" href="/blog/assets/js/161.97acae68.js"><link rel="prefetch" href="/blog/assets/js/162.86431056.js"><link rel="prefetch" href="/blog/assets/js/163.f1bfa2fe.js"><link rel="prefetch" href="/blog/assets/js/164.105b48fa.js"><link rel="prefetch" href="/blog/assets/js/165.700f83da.js"><link rel="prefetch" href="/blog/assets/js/166.8de21024.js"><link rel="prefetch" href="/blog/assets/js/167.2b670c3d.js"><link rel="prefetch" href="/blog/assets/js/168.330ac31c.js"><link rel="prefetch" href="/blog/assets/js/169.f8233c38.js"><link rel="prefetch" href="/blog/assets/js/17.93f492a1.js"><link rel="prefetch" href="/blog/assets/js/170.7c8b0366.js"><link rel="prefetch" href="/blog/assets/js/171.c3155533.js"><link rel="prefetch" href="/blog/assets/js/172.b659d767.js"><link rel="prefetch" href="/blog/assets/js/173.62c681db.js"><link rel="prefetch" href="/blog/assets/js/174.5c66f092.js"><link rel="prefetch" href="/blog/assets/js/175.d41dd28b.js"><link rel="prefetch" href="/blog/assets/js/176.e60d7f0a.js"><link rel="prefetch" href="/blog/assets/js/177.10de95b1.js"><link rel="prefetch" href="/blog/assets/js/178.f301674d.js"><link rel="prefetch" href="/blog/assets/js/179.4d54a4ff.js"><link rel="prefetch" href="/blog/assets/js/18.f17de23a.js"><link rel="prefetch" href="/blog/assets/js/180.955aa8ec.js"><link rel="prefetch" href="/blog/assets/js/181.a53e32e0.js"><link rel="prefetch" href="/blog/assets/js/182.38687994.js"><link rel="prefetch" href="/blog/assets/js/183.544fef00.js"><link rel="prefetch" href="/blog/assets/js/184.711e54a6.js"><link rel="prefetch" href="/blog/assets/js/185.20075148.js"><link rel="prefetch" href="/blog/assets/js/186.08c67f20.js"><link rel="prefetch" href="/blog/assets/js/187.7ca2d0c6.js"><link rel="prefetch" href="/blog/assets/js/188.cd167879.js"><link rel="prefetch" href="/blog/assets/js/189.e8e2eb21.js"><link rel="prefetch" href="/blog/assets/js/19.6b963460.js"><link rel="prefetch" href="/blog/assets/js/190.8b557318.js"><link rel="prefetch" href="/blog/assets/js/191.3e6fdc4f.js"><link rel="prefetch" href="/blog/assets/js/192.99d6e1d8.js"><link rel="prefetch" href="/blog/assets/js/193.4ced091e.js"><link rel="prefetch" href="/blog/assets/js/194.4b375e2e.js"><link rel="prefetch" href="/blog/assets/js/195.003e3d67.js"><link rel="prefetch" href="/blog/assets/js/196.7a3f55e5.js"><link rel="prefetch" href="/blog/assets/js/197.30d4c5b4.js"><link rel="prefetch" href="/blog/assets/js/198.628c2c1a.js"><link rel="prefetch" href="/blog/assets/js/199.89c4e586.js"><link rel="prefetch" href="/blog/assets/js/20.839dae41.js"><link rel="prefetch" href="/blog/assets/js/200.1d9f2ef9.js"><link rel="prefetch" href="/blog/assets/js/201.5ba078d9.js"><link rel="prefetch" href="/blog/assets/js/202.b49b23f4.js"><link rel="prefetch" href="/blog/assets/js/203.d88a03d9.js"><link rel="prefetch" href="/blog/assets/js/204.fb928277.js"><link rel="prefetch" href="/blog/assets/js/205.432c3d8d.js"><link rel="prefetch" href="/blog/assets/js/206.ed726599.js"><link rel="prefetch" href="/blog/assets/js/207.9ac30d7b.js"><link rel="prefetch" href="/blog/assets/js/208.c5e77adc.js"><link rel="prefetch" href="/blog/assets/js/209.d396aad5.js"><link rel="prefetch" href="/blog/assets/js/21.bd21bd29.js"><link rel="prefetch" href="/blog/assets/js/210.02e4ee2f.js"><link rel="prefetch" href="/blog/assets/js/211.0f8a9304.js"><link rel="prefetch" href="/blog/assets/js/212.9220dd3d.js"><link rel="prefetch" href="/blog/assets/js/213.89521ebd.js"><link rel="prefetch" href="/blog/assets/js/214.cd5ec468.js"><link rel="prefetch" href="/blog/assets/js/215.0084d772.js"><link rel="prefetch" href="/blog/assets/js/216.a72becb1.js"><link rel="prefetch" href="/blog/assets/js/217.71a7f3aa.js"><link rel="prefetch" href="/blog/assets/js/218.9793e19a.js"><link rel="prefetch" href="/blog/assets/js/219.c89175e9.js"><link rel="prefetch" href="/blog/assets/js/22.e6bcf65d.js"><link rel="prefetch" href="/blog/assets/js/220.d496e411.js"><link rel="prefetch" href="/blog/assets/js/221.07a9338a.js"><link rel="prefetch" href="/blog/assets/js/222.22bf261f.js"><link rel="prefetch" href="/blog/assets/js/223.b9c6a055.js"><link rel="prefetch" href="/blog/assets/js/224.364b4b11.js"><link rel="prefetch" href="/blog/assets/js/225.adec6660.js"><link rel="prefetch" href="/blog/assets/js/226.78192713.js"><link rel="prefetch" href="/blog/assets/js/227.0c1ec0e5.js"><link rel="prefetch" href="/blog/assets/js/228.a94ad835.js"><link rel="prefetch" href="/blog/assets/js/229.51820867.js"><link rel="prefetch" href="/blog/assets/js/23.7bb82fc7.js"><link rel="prefetch" href="/blog/assets/js/230.6eb1d9bf.js"><link rel="prefetch" href="/blog/assets/js/231.55d62061.js"><link rel="prefetch" href="/blog/assets/js/232.f938a3a8.js"><link rel="prefetch" href="/blog/assets/js/233.2c81f1b3.js"><link rel="prefetch" href="/blog/assets/js/234.4dbf5d59.js"><link rel="prefetch" href="/blog/assets/js/235.f78274d9.js"><link rel="prefetch" href="/blog/assets/js/236.a2b61bec.js"><link rel="prefetch" href="/blog/assets/js/237.6ae31c88.js"><link rel="prefetch" href="/blog/assets/js/238.2f5c56ae.js"><link rel="prefetch" href="/blog/assets/js/239.506a4e9f.js"><link rel="prefetch" href="/blog/assets/js/24.2c9c4ee6.js"><link rel="prefetch" href="/blog/assets/js/25.b4de33d1.js"><link rel="prefetch" href="/blog/assets/js/26.0bb98ba9.js"><link rel="prefetch" href="/blog/assets/js/27.df98327e.js"><link rel="prefetch" href="/blog/assets/js/28.31289bac.js"><link rel="prefetch" href="/blog/assets/js/29.45af5621.js"><link rel="prefetch" href="/blog/assets/js/30.d5c08e66.js"><link rel="prefetch" href="/blog/assets/js/31.78e43a68.js"><link rel="prefetch" href="/blog/assets/js/32.66c0e717.js"><link rel="prefetch" href="/blog/assets/js/33.0efe6b11.js"><link rel="prefetch" href="/blog/assets/js/34.cb1866c1.js"><link rel="prefetch" href="/blog/assets/js/35.dca9b927.js"><link rel="prefetch" href="/blog/assets/js/36.fb4476c3.js"><link rel="prefetch" href="/blog/assets/js/37.09dfc1c5.js"><link rel="prefetch" href="/blog/assets/js/38.6624bf02.js"><link rel="prefetch" href="/blog/assets/js/39.df6c26ac.js"><link rel="prefetch" href="/blog/assets/js/4.44654b1a.js"><link rel="prefetch" href="/blog/assets/js/40.80101c19.js"><link rel="prefetch" href="/blog/assets/js/41.2b5e8c27.js"><link rel="prefetch" href="/blog/assets/js/42.c6ded3fe.js"><link rel="prefetch" href="/blog/assets/js/43.6d9424d6.js"><link rel="prefetch" href="/blog/assets/js/44.835e4b5c.js"><link rel="prefetch" href="/blog/assets/js/45.e7bb6f6a.js"><link rel="prefetch" href="/blog/assets/js/46.1b9286bf.js"><link rel="prefetch" href="/blog/assets/js/47.8d66ca97.js"><link rel="prefetch" href="/blog/assets/js/48.3c1102e1.js"><link rel="prefetch" href="/blog/assets/js/49.e17a3436.js"><link rel="prefetch" href="/blog/assets/js/5.88de390f.js"><link rel="prefetch" href="/blog/assets/js/50.6750f186.js"><link rel="prefetch" href="/blog/assets/js/51.9f93af9f.js"><link rel="prefetch" href="/blog/assets/js/52.f3ef3b5e.js"><link rel="prefetch" href="/blog/assets/js/53.a6bacd25.js"><link rel="prefetch" href="/blog/assets/js/54.dbb7c9ab.js"><link rel="prefetch" href="/blog/assets/js/55.2562d0c8.js"><link rel="prefetch" href="/blog/assets/js/56.14ea4931.js"><link rel="prefetch" href="/blog/assets/js/57.a2fad780.js"><link rel="prefetch" href="/blog/assets/js/58.8165b971.js"><link rel="prefetch" href="/blog/assets/js/59.556cab0d.js"><link rel="prefetch" href="/blog/assets/js/6.277038ca.js"><link rel="prefetch" href="/blog/assets/js/60.f048aa7c.js"><link rel="prefetch" href="/blog/assets/js/61.bdb307a8.js"><link rel="prefetch" href="/blog/assets/js/62.37a94f10.js"><link rel="prefetch" href="/blog/assets/js/63.74811780.js"><link rel="prefetch" href="/blog/assets/js/64.d3429318.js"><link rel="prefetch" href="/blog/assets/js/65.acfdd371.js"><link rel="prefetch" href="/blog/assets/js/66.cb805d9b.js"><link rel="prefetch" href="/blog/assets/js/67.39f85baa.js"><link rel="prefetch" href="/blog/assets/js/68.a1902a2a.js"><link rel="prefetch" href="/blog/assets/js/69.923fdd78.js"><link rel="prefetch" href="/blog/assets/js/7.474f8f8d.js"><link rel="prefetch" href="/blog/assets/js/70.1f3e978d.js"><link rel="prefetch" href="/blog/assets/js/71.13cd9358.js"><link rel="prefetch" href="/blog/assets/js/72.739b22a8.js"><link rel="prefetch" href="/blog/assets/js/73.95f69ae2.js"><link rel="prefetch" href="/blog/assets/js/74.b6624f6a.js"><link rel="prefetch" href="/blog/assets/js/75.31b56e72.js"><link rel="prefetch" href="/blog/assets/js/76.b9c0ce40.js"><link rel="prefetch" href="/blog/assets/js/77.46f6e413.js"><link rel="prefetch" href="/blog/assets/js/78.aebd00ee.js"><link rel="prefetch" href="/blog/assets/js/79.1b784d15.js"><link rel="prefetch" href="/blog/assets/js/8.9428e7ee.js"><link rel="prefetch" href="/blog/assets/js/80.1f550d53.js"><link rel="prefetch" href="/blog/assets/js/81.101cc131.js"><link rel="prefetch" href="/blog/assets/js/82.077c8298.js"><link rel="prefetch" href="/blog/assets/js/83.2e375d11.js"><link rel="prefetch" href="/blog/assets/js/84.38102a34.js"><link rel="prefetch" href="/blog/assets/js/85.24532d6a.js"><link rel="prefetch" href="/blog/assets/js/86.1dabbf00.js"><link rel="prefetch" href="/blog/assets/js/87.763da0f2.js"><link rel="prefetch" href="/blog/assets/js/88.ff6e5f7c.js"><link rel="prefetch" href="/blog/assets/js/89.187e5e16.js"><link rel="prefetch" href="/blog/assets/js/9.da143545.js"><link rel="prefetch" href="/blog/assets/js/90.3c8cff94.js"><link rel="prefetch" href="/blog/assets/js/91.a50bd44d.js"><link rel="prefetch" href="/blog/assets/js/92.5484868f.js"><link rel="prefetch" href="/blog/assets/js/93.c8ee75e3.js"><link rel="prefetch" href="/blog/assets/js/94.b18a3e9b.js"><link rel="prefetch" href="/blog/assets/js/95.cddef6ae.js"><link rel="prefetch" href="/blog/assets/js/96.80e5a938.js"><link rel="prefetch" href="/blog/assets/js/97.1f5e5197.js"><link rel="prefetch" href="/blog/assets/js/98.e3a275c8.js"><link rel="prefetch" href="/blog/assets/js/99.d33bf89e.js">
  13. <link rel="stylesheet" href="/blog/assets/css/0.styles.cf8cd190.css">
  14. </head>
  15. <body class="theme-mode-light">
  16. <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/img/logo.png" alt="彪哥博客" class="logo"> <span class="site-name can-hide">彪哥博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="http://fseller.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  17. 个人游戏网站
  18. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/heBody/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
  19. GitHub
  20. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://fastly.jsdelivr.net/gh/xugaoyi/image_store/blog/20200103123203.jpg"> <div class="blogger-info"><h3>彪哥</h3> <span>爱好前端</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="http://fseller.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  21. 个人游戏网站
  22. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/heBody/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
  23. GitHub
  24. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><a href="/blog/pages/0796ba76b4b55368/" class="sidebar-link">基础</a></li><li><a href="/blog/pages/74d2ab3fbfeaaa68/" aria-current="page" class="active sidebar-link">内置对象</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#一、object对象" class="sidebar-link">一、Object对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1、概述" class="sidebar-link">1、概述</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2、object-当作工具方法-不通过new创建" class="sidebar-link">2、Object() 当作工具方法(不通过new创建)</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3、object构造函数-通过new创建" class="sidebar-link">3、Object构造函数(通过new创建)</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4、object-的静态方法" class="sidebar-link">4、Object 的静态方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-keys" class="sidebar-link">Object.keys()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-getownpropertynames" class="sidebar-link">Object.getOwnPropertyNames()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object的其他静态方法" class="sidebar-link">Object的其他静态方法</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1-对象属性模型的相关方法" class="sidebar-link">(1)对象属性模型的相关方法</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-控制对象状态的方法" class="sidebar-link">(2)控制对象状态的方法</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-原型链相关方法" class="sidebar-link">(3)原型链相关方法</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5、object-的实例方法" class="sidebar-link">5、Object 的实例方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-prototype-valueof" class="sidebar-link">Object.prototype.valueOf()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-prototype-tostring" class="sidebar-link">Object.prototype.toString()</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#tostring-的应用-判断数据类型" class="sidebar-link">toString() 的应用:判断数据类型</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-prototype-tolocalestring" class="sidebar-link">Object.prototype.toLocaleString()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-prototype-hasownproperty" class="sidebar-link">Object.prototype.hasOwnProperty()</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#二、属性描述对象" class="sidebar-link">二、属性描述对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1、概述-2" class="sidebar-link">1、概述</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2、object-getownpropertydescriptor" class="sidebar-link">2、Object.getOwnPropertyDescriptor()</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3、object-getownpropertynames" class="sidebar-link">3、Object.getOwnPropertyNames()</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4、object-defineproperty-object-defineproperties" class="sidebar-link">4、Object.defineProperty(),Object.defineProperties()</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5、object-prototype-propertyisenumerable" class="sidebar-link">5、Object.prototype.propertyIsEnumerable()</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_6、元属性" class="sidebar-link">6、元属性</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#value目标属性的值" class="sidebar-link">value目标属性的值</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#writable可写性" class="sidebar-link">writable可写性</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#enumerable可遍历性" class="sidebar-link">enumerable可遍历性</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#configurable可配置性" class="sidebar-link">configurable可配置性</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_7、存取器-get和set" class="sidebar-link">7、存取器(get和set)</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_8、对象的拷贝" class="sidebar-link">8、对象的拷贝</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_9、控制对象读写状态" class="sidebar-link">9、控制对象读写状态</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-preventextensions-防止新增" class="sidebar-link">Object.preventExtensions() 防止新增</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-isextensible-是可新增的" class="sidebar-link">Object.isExtensible() 是可新增的?</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-seal-密封" class="sidebar-link">Object.seal() 密封</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-issealed-是密封的" class="sidebar-link">Object.isSealed() 是密封的?</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-freeze-冻结" class="sidebar-link">Object.freeze() 冻结</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#object-isfrozen-是冻结的" class="sidebar-link">Object.isFrozen() 是冻结的?</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#局限性" class="sidebar-link">局限性</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#三、array对象" class="sidebar-link">三、Array对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1、array构造函数" class="sidebar-link">1、Array构造函数</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2、静态方法" class="sidebar-link">2、静态方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#array-isarray" class="sidebar-link">Array.isArray()</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3、实例方法" class="sidebar-link">3、实例方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#valueof-原数组-tostring-转字符串" class="sidebar-link">valueOf() 原数组,toString() 转字符串</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#push-末尾添加-pop-删除末尾" class="sidebar-link">push() 末尾添加,pop() 删除末尾</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#unshift-首位添加-shift-删除首位" class="sidebar-link">unshift() 首位添加, shift() 删除首位</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#join-连接每个成员-转字符串" class="sidebar-link">join() 连接每个成员-转字符串</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#concat-合并数组" class="sidebar-link">concat() 合并数组</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#reverse-反转" class="sidebar-link">reverse() 反转</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#slice-部分-提取部分成员" class="sidebar-link">slice() 部分-提取部分成员</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#应用-类数组转数组" class="sidebar-link">应用-类数组转数组</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#splice-从第几个开始-删除几个-并添加成员-返回被删除成员。" class="sidebar-link">splice() 从第几个开始,删除几个,并添加成员,返回被删除成员。</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#应用-拆分数组" class="sidebar-link">应用-拆分数组</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#sort-排序" class="sidebar-link">sort() 排序</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#应用-自定义排序" class="sidebar-link">应用-自定义排序</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#map-映射-每个成员-并返回" class="sidebar-link">map() 映射 每个成员 并返回</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#foreach-循环每个成员" class="sidebar-link">forEach() 循环每个成员</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#filter-过滤" class="sidebar-link">filter() 过滤</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#some-某个-every-每个" class="sidebar-link">some()某个,every()每个</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#reduce-累计器-reduceright-反向累计器" class="sidebar-link">reduce() 累计器,reduceRight() 反向累计器</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#应用-找出字符最长的数组成员" class="sidebar-link">应用-找出字符最长的数组成员</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#应用-按属性对object分类" class="sidebar-link">应用-按属性对object分类</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#indexof-搜索成员第一个出现的位置-lastindexof-搜索成员最后出现的位置" class="sidebar-link">indexOf() 搜索成员第一个出现的位置,lastIndexOf() 搜索成员最后出现的位置</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#链式使用" class="sidebar-link">链式使用</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#四、包装对象" class="sidebar-link">四、包装对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1、定义" class="sidebar-link">1、定义</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2、实例方法" class="sidebar-link">2、实例方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-1-valueof" class="sidebar-link">2.1 valueOf()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-2-tostring" class="sidebar-link">2.2 toString()</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3、原始类型与实例对象的自动转换" class="sidebar-link">3、原始类型与实例对象的自动转换</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4、自定义方法" class="sidebar-link">4、自定义方法</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#五、boolean对象" class="sidebar-link">五、Boolean对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1、概述-3" class="sidebar-link">1、概述</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2、boolean-函数的类型转换作用" class="sidebar-link">2、Boolean 函数的类型转换作用</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#六、number对象" class="sidebar-link">六、Number对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1、概述-4" class="sidebar-link">1、概述</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2、静态属性" class="sidebar-link">2、静态属性</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3、实例方法-2" class="sidebar-link">3、实例方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-1-number-prototype-tostring-可转成多种进制字符串" class="sidebar-link">3.1 Number.prototype.toString() 可转成多种进制字符串</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-2-number-prototype-tofixed-转成保留指定小数位数的字符串" class="sidebar-link">3.2 Number.prototype.toFixed() 转成保留指定小数位数的字符串</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-3-number-prototype-toexponential-转成科学计数法形式字符串" class="sidebar-link">3.3 Number.prototype.toExponential() 转成科学计数法形式字符串</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-4-number-prototype-toprecision-转为指定位数的有效数字" class="sidebar-link">3.4 Number.prototype.toPrecision()转为指定位数的有效数字</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-5-number-prototype-tolocalestring" class="sidebar-link">4.5 Number.prototype.toLocaleString()</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#应用-添加千位分隔符" class="sidebar-link">应用-添加千位分隔符</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4、自定义方法-2" class="sidebar-link">4、自定义方法</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#七、string对象" class="sidebar-link">七、String对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1、概述-5" class="sidebar-link">1、概述</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2、静态方法-2" class="sidebar-link">2、静态方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#string-fromcharcode" class="sidebar-link">String.fromCharCode()</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3、实例属性" class="sidebar-link">3、实例属性</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#string-prototype-length" class="sidebar-link">String.prototype.length</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4、实例方法" class="sidebar-link">4、实例方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-1-string-prototype-charat-返回指定位置的字符" class="sidebar-link">4.1 String.prototype.charAt() 返回指定位置的字符</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-2-string-prototype-charcodeat-返回字符串指定位置的-unicode-码点-十进制表示" class="sidebar-link">4.2 String.prototype.charCodeAt()返回字符串指定位置的 Unicode 码点(十进制表示)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-3-string-prototype-concat-合并字符串-返回新字符串-不改变原字符串" class="sidebar-link">4.3 String.prototype.concat() 合并字符串,返回新字符串,不改变原字符串</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-4-string-prototype-slice-取出部分子字符串-不改变原字符串" class="sidebar-link">4.4 String.prototype.slice() 取出部分子字符串,不改变原字符串</a></li><li class="sidebar-sub-header level5"><a href="/blog/pages/74d2ab3fbfeaaa68/#负数表示倒数" class="sidebar-link">负数表示倒数</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-5-string-prototype-substring-和slice类似" class="sidebar-link">4.5 String.prototype.substring() 和slice类似</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#小总结-substring-和-slice-的区别-有两个。" class="sidebar-link">小总结:substring() 和 slice() 的区别,有两个。</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-6-string-prototype-substr-和-slice、substring类似-区别是参数二表示长度" class="sidebar-link">4.6 String.prototype.substr() 和 slice、substring类似,区别是参数二表示长度</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-7-string-prototype-indexof-搜索子串第一个出现的位置-string-prototype-lastindexof-搜索子串最后出现的位置" class="sidebar-link">4.7 String.prototype.indexOf() 搜索子串第一个出现的位置,String.prototype.lastIndexOf() 搜索子串最后出现的位置</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-8-string-prototype-trim-修剪-两端空格-不改变原字符串" class="sidebar-link">4.8 String.prototype.trim() ''修剪'‘两端空格,不改变原字符串</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-9-string-prototype-tolowercase-全转小写-string-prototype-touppercase-全转大写-都不改变原字符串" class="sidebar-link">4.9 String.prototype.toLowerCase() 全转小写,String.prototype.toUpperCase() 全转大写,都不改变原字符串</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-10-string-prototype-match-匹配子串-返回数组-数组带index、input属性" class="sidebar-link">4.10 String.prototype.match() 匹配子串,返回数组(数组带index、input属性)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-11-string-prototype-search-搜索子串第一次出现的位置-没有则返-1-string-prototype-replace-替换子串-参数二替换参数一-不改变原字符串" class="sidebar-link">4.11 String.prototype.search() 搜索子串第一次出现的位置,没有则返-1,String.prototype.replace() 替换子串,参数二替换参数一,不改变原字符串</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-12-string-prototype-split-按给定规则分割字符串-返回数组" class="sidebar-link">4.12 String.prototype.split() 按给定规则分割字符串,返回数组</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-13-string-prototype-localecompare-比较两个字符串-返回整数。" class="sidebar-link">4.13 String.prototype.localeCompare() 比较两个字符串,返回整数。</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#八、math对象" class="sidebar-link">八、Math对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1、静态属性" class="sidebar-link">1、静态属性</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2、静态方法-3" class="sidebar-link">2、静态方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-1-math-abs-绝对值" class="sidebar-link">2.1 Math.abs() 绝对值</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-2-math-max-最大值-math-min-最-小值" class="sidebar-link">2.2 Math.max() 最大值,Math.min()最 小值</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-3-math-floor-向下取整-math-ceil-向上取整" class="sidebar-link">2.3 Math.floor() 向下取整,Math.ceil() 向上取整</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-4-math-round-四舍五入" class="sidebar-link">2.4 Math.round() 四舍五入</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-5-math-pow-指数运算-次方运算" class="sidebar-link">2.5 Math.pow() 指数运算 (次方运算)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-6-math-sqrt-平方根" class="sidebar-link">2.6 Math.sqrt() 平方根</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-7-math-log-自然对数" class="sidebar-link">2.7 Math.log() 自然对数</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-8-math-exp-e的指数" class="sidebar-link">2.8 Math.exp() e的指数</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-9-math-random-0到1的随机数-可能等于0-但一定小于1" class="sidebar-link">2.9 Math.random() 0到1的随机数(可能等于0,但一定小于1)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-10-三角函数方法" class="sidebar-link">2.10 三角函数方法</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#九、date对象" class="sidebar-link">九、Date对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1、普通函数的用法" class="sidebar-link">1、普通函数的用法</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2、构造函数的用法" class="sidebar-link">2、构造函数的用法</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3、日期的运算" class="sidebar-link">3、日期的运算</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4、静态方法" class="sidebar-link">4、静态方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-1-date-now-当前时间戳" class="sidebar-link">4.1 Date.now() 当前时间戳</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-2-date-parse-解析日期字符串-返回该时间点的时间戳" class="sidebar-link">4.2 Date.parse() 解析日期字符串,返回该时间点的时间戳</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-3-date-utc-以年、月、日等变量作为参数-返回该时间点的时间戳" class="sidebar-link">4.3 Date.UTC() 以年、月、日等变量作为参数,返回该时间点的时间戳</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5、实例方法" class="sidebar-link">5、实例方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-1-date-prototype-valueof-返回时间戳-等同于gettime-。-减法运算默认调用" class="sidebar-link">5.1 Date.prototype.valueOf() 返回时间戳,等同于getTime()。(减法运算默认调用)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-2-to-类方法-返回字符串相关" class="sidebar-link">5.2 to 类方法(返回字符串相关)</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1-date-prototype-tostring-返回一个完整的日期字符串-直接读取实例默认调用此方法" class="sidebar-link">(1)Date.prototype.toString() 返回一个完整的日期字符串(直接读取实例默认调用此方法)</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-date-prototype-toutcstring-返回对应的utc时间-世界标准时-比北京时间晚8小时" class="sidebar-link">(2)Date.prototype.toUTCString() 返回对应的UTC时间(世界标准时),比北京时间晚8小时</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-date-prototype-toisostring-返回对应时间的-iso8601-写法-utc时间" class="sidebar-link">(3)Date.prototype.toISOString() 返回对应时间的 ISO8601 写法(UTC时间)</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-date-prototype-tojson-返回符合json格式字符串-与toisostring方法的返回结果完全相同。" class="sidebar-link">(4)Date.prototype.toJSON() 返回符合JSON格式字符串,与toISOString方法的返回结果完全相同。</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-date-prototype-todatestring-返回日期字符串-不含时分秒" class="sidebar-link">(5)Date.prototype.toDateString() 返回日期字符串(不含时分秒)</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_6-date-prototype-totimestring-返回时间字符串-不含年月日" class="sidebar-link">(6)Date.prototype.toTimeString() 返回时间字符串(不含年月日)</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_7-本地时间" class="sidebar-link">(7)本地时间</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-3-get类-获取年、月、日、时、分、秒等" class="sidebar-link">5.3 get类 (获取年、月、日、时、分、秒等)</a></li><li class="sidebar-sub-header level6"><a href="/blog/pages/74d2ab3fbfeaaa68/#例子-计算本年度还剩下多少天" class="sidebar-link">例子:计算本年度还剩下多少天</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-4-set-类方法" class="sidebar-link">5.4 set 类方法</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#十、regexp-对象" class="sidebar-link">十、RegExp 对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1、概述-6" class="sidebar-link">1、概述</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2、实例属性" class="sidebar-link">2、实例属性</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3、实例方法-3" class="sidebar-link">3、实例方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-1-regexp-prototype-test-当前模式是否匹配参数字符串-返回布尔值" class="sidebar-link">3.1 RegExp.prototype.test() 当前模式是否匹配参数字符串,返回布尔值</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-2-regexp-prototype-exec-返回匹配结果-如匹配则返回一个数组-成员是匹配到的子字符串-否则返回null" class="sidebar-link">3.2 RegExp.prototype.exec() 返回匹配结果,如匹配则返回一个数组,成员是匹配到的子字符串,否则返回null</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4、字符串的实例方法" class="sidebar-link">4、字符串的实例方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-1-string-prototype-match-匹配-返回匹配结果数组或null" class="sidebar-link">4.1 String.prototype.match() 匹配,返回匹配结果数组或null</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-2-string-prototype-search-返回第一个满足匹配条件的位置-不满足则返-1" class="sidebar-link">4.2 String.prototype.search() 返回第一个满足匹配条件的位置,不满足则返-1</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-3-string-prototype-replace-替换匹配的值-参数一是子串或正则-参数二是替换内容" class="sidebar-link">4.3 String.prototype.replace() 替换匹配的值,参数一是子串或正则,参数二是替换内容</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-4-string-prototype-split-按给定规则分割字符串-返回数组" class="sidebar-link">4.4 String.prototype.split() 按给定规则分割字符串,返回数组</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5、匹配规则" class="sidebar-link">5、匹配规则</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-1-字面量字符和元字符" class="sidebar-link">5.1 字面量字符和元字符</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1-点字符-类似通配" class="sidebar-link">(1)点字符(.) 类似通配</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-位置字符-开始位置和结束位置" class="sidebar-link">(2)位置字符(^) ($) 开始位置和结束位置</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-选择符-表示-或关系" class="sidebar-link">(3)选择符(|) 表示''或关系''</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-2-转义符-反斜杠" class="sidebar-link">5.2 转义符(\ 反斜杠)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-3-特殊字符" class="sidebar-link">5.3 特殊字符</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-4-字符类" class="sidebar-link">5.4 字符类</a></li><li class="sidebar-sub-header level5"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1-脱字符-排除" class="sidebar-link">(1)脱字符(^ 排除)</a></li><li class="sidebar-sub-header level5"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-连字符-字符连续范围" class="sidebar-link">(2)连字符(- 字符连续范围)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-5-预定义模式-常见模式简写-d-d-w-w-s-s-b-b" class="sidebar-link">5.5 预定义模式 (常见模式简写\d\D\w\W\s\S\b\B)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-6-重复类-n-n-n-m" class="sidebar-link">5.6 重复类 ( {n} {n,} {n,m} )</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-7-量词符" class="sidebar-link">5.7 量词符(?)(*)(+)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-8-贪婪模式-对量词符可使用非贪婪模式" class="sidebar-link">5.8 贪婪模式(对量词符可使用非贪婪模式)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-9-修饰符" class="sidebar-link">5.9 修饰符</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1-g-修饰符-全局匹配" class="sidebar-link">(1)g 修饰符 (全局匹配)</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-i-修饰符-不区分大小写" class="sidebar-link">(2)i 修饰符 (不区分大小写)</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-m-修饰符-让-和-识别换行符" class="sidebar-link">(3)m 修饰符(让^和$识别换行符)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_5-10-组匹配" class="sidebar-link">5.10 组匹配</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1-概述-圆括号分组" class="sidebar-link">(1)概述 (圆括号分组)</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2-非捕获组" class="sidebar-link">(2)非捕获组</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-先行断言" class="sidebar-link">(3)先行断言</a></li><li class="sidebar-sub-header level4"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4-先行否定断言" class="sidebar-link">(4)先行否定断言</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#十一、json对象" class="sidebar-link">十一、JSON对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_1、json-格式" class="sidebar-link">1、JSON 格式</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_2、json-对象" class="sidebar-link">2、JSON 对象</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3、json-stringify-转为json字符串" class="sidebar-link">3、JSON.stringify() 转为JSON字符串</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-1-基本用法" class="sidebar-link">3.1 基本用法</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-2-第二个参数-一个数组-对象属性白名单-或一个函数-改写返回值" class="sidebar-link">3.2 第二个参数 (一个数组,对象属性白名单;或一个函数,改写返回值)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-3-第三个参数-增加返回json字符串的可读性-json格式" class="sidebar-link">3.3 第三个参数 (增加返回JSON字符串的可读性;json格式)</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/74d2ab3fbfeaaa68/#_3-4-参数对象的-tojson-方法-对象自定义tojson方法-转字符串只取该方法返回值" class="sidebar-link">3.4 参数对象的 toJSON 方法(对象自定义toJSON方法,转字符串只取该方法返回值)</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#_4、json-parse" class="sidebar-link">4、JSON.parse()</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/74d2ab3fbfeaaa68/#文档" class="sidebar-link">文档</a></li></ul></li><li><a href="/blog/pages/659b5af5e2e704e0/" class="sidebar-link">面向对象</a></li><li><a href="/blog/pages/d61b1cb4cdac1f63/" class="sidebar-link">异步操作</a></li><li><a href="/blog/pages/7d961b8030c6099e/" class="sidebar-link">DOM</a></li><li><a href="/blog/pages/10b2761db5a8e089/" class="sidebar-link">事件</a></li><li><a href="/blog/pages/bab4930124ad2c10/" class="sidebar-link">浏览器模型</a></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06970110><div class="articleInfo" data-v-06970110><ul class="breadcrumbs" data-v-06970110><li data-v-06970110><a href="/blog/" title="首页" class="iconfont icon-home router-link-active" data-v-06970110></a></li> <li data-v-06970110><a href="/blog/note/javascript/#《JavaScript教程》笔记" data-v-06970110>《JavaScript教程》笔记</a></li></ul> <div class="info" data-v-06970110><div title="作者" class="author iconfont icon-touxiang" data-v-06970110><a href="https://github.com/heBody" target="_blank" title="作者" class="beLink" data-v-06970110>heBody</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06970110><a href="javascript:;" data-v-06970110>2020-01-12</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAAAXNSR0IArs4c6QAABH1JREFUSA3tVl1oHFUUPmdmd2ltklqbpJDiNnXFmgbFktho7YMPNiJSSZM0+CAYSkUELVhM6YuwIPpgoOKDqOBDC0XE2CQoNtQXBUFTTcCi+Wlh1V2TQExsUzcltd3M9Tt3ZjZzZ2fT+OJTL8yeM+eee757fmeJbq//KQL8X3DUSFOcfr7cRsRtxNQMWueeVzOkaITIGqQHNg5y8+jNW9ldM7A6nTpAjuolUikAwq7CE3WcM2RRDz+XGVgN3FptU/aUSlvq9Pa3iZ1+sgAqJyyAFqkipd9dqiwHF3P65YycLWc/6sqGrvoEoIp6DOFaX5h6+dnfjkWprwqsPk0dUGq5vySwDImC10KxFHgGL1SWoc92O3eVht09qdXNH11I2SsTsJYqMWzihqGMi+A+Garf3BAuuLI5oGlULyNfyB/HYNujwktOfRrMr5t77NmevqaUopx0grnKAyvVpmwUDB4x6FPXuGvYLTDwWsejwgtgkYKPqRJg8SV6xaiZ3ZTppGneS4yfH5/66fZSDHv+QZci/+h5c5UHtpy67JUqGppM0sh0Nc1dW6/N1W5Yoqat8/TU/VnadmdeW2PLLSyh0cvxBs3KbqTmwYPpxN4do/mzE8nEpvX/UMu2Wbp74zUAK5q6WkHns7V0eWkdPbPzd3rxkTGybadYySumVzhcaJFbs5UrEkQ/+CK8gF5dnh/6ciIZ73gwQ927L1IitoxKLXYP3SjYdOrHHfTZhRRlFyrorafPk20B3HPD1y2G3qKZME5Jcf3t/HUC13/8tSd++vqFveMUTwAUxSUFI1QekR1+bIze3D9MF2aq6cPvG72CgnldWCFqyRw3lwH8ZMerjTD9ElRO7Gv44wNpC90aASqGfVlz/Rx17srQ57/UU26hkhQqUB7dBR71WmzQhHUnblGmVOEw0jhbV1n9OlXUDCIRGaNV5Jp43N516fN7JmnTHdfp7Hgy0luO4aMhtkLL8Bi3bUWYvzh5Mn1dTxrL6QmGuRhGL/TiTTxRoEdTszSaq9GR0NGA3KdkOz3hqSV3MIDhQ5IVX/Ivx3umBti2es2h4eZby7x8br1rkf7Mo90AqC8aQ3sJeNzqFRu+vSANAQe3PL7l0HGOAdwDCeZYvNKeoZp1Qfs6Aipndh86HmFRi0LAnEO47wsqM6cdfjh3jBPUzhZy7nvlUfFsamED1VQt6aISHVymXZ/B2aCtIG8AI8xfobj2d3en1wWVhOeHELKmLQ1s211s88comkv4UCwWyF787mJdYXtNfhKAXVqnKTq8QZvGAGGOfaTo5pGZ/PwbUCr5+DPr/1J92JNHr9aOl/F3iI5+O1nfybsGxoimvZ3ViWSluDITw3P37mypheDIPY0tw7+O/5ApbkYw+zpfaUVu32Pi98+defdUhEpZkRFq0aqyNh9FuL9hpYbEm6iwi0z2REd09ZmyENEbuhjDWzKvZXTqKYaBIr3tt5kuPtQBZFvEUwHt60vfCNu41XsksH9Ij1BMMz1Y0OOunHNShFIP5868g5zeXmuLwL9T4b6Q2+KejgAAAABJRU5ErkJggg==">内置对象<!----></h1> <div class="theme-vdoing-content content__default"><h1 id="内置对象篇"><a href="#内置对象篇" class="header-anchor">#</a> 内置对象篇</h1> <h2 id="一、object对象"><a href="#一、object对象" class="header-anchor">#</a> 一、Object对象</h2> <h2 id="_1、概述"><a href="#_1、概述" class="header-anchor">#</a> 1、概述</h2> <p>JavaScript 原生提供<code>Object</code>对象(注意起首的<code>O</code>是大写),本章介绍该对象原生的各种方法。</p> <p>JavaScript 的所有其他对象都继承自<code>Object</code>对象,即那些对象都是<code>Object</code>的实例。</p> <p><code>Object</code>对象的原生方法分成两类:<code>Object</code>本身的方法与<code>Object</code>的实例方法。</p> <p><strong>(1)Object对象本身的方法</strong></p> <p>所谓“本身的方法”就是直接定义在<code>Object</code>对象的方法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Object<span class="token punctuation">.</span><span class="token function-variable function">print</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">o</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
  25. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中,<code>print</code>方法就是直接定义在<code>Object</code>对象上。</p> <p><strong>(2)Object的实例方法</strong></p> <p>所谓实例方法就是定义在<code>Object</code>原型对象<code>Object.prototype</code>上的方法。它可以被<code>Object</code>实例直接使用。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">print</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  26. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  27. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  28. <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  29. obj<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// Object</span>
  30. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,<code>Object.prototype</code>定义了一个<code>print</code>方法,然后生成一个<code>Object</code>的实例<code>obj</code>。<code>obj</code>直接继承了<code>Object.prototype</code>的属性和方法,可以直接使用<code>obj.print</code>调用<code>print</code>方法。也就是说,<code>obj</code>对象的<code>print</code>方法实质上就是调用<code>Object.prototype.print</code>方法。</p> <p>关于原型对象<code>object.prototype</code>的详细解释,参见《面向对象编程》章节。这里只要知道,凡是定义在<code>Object.prototype</code>对象上面的属性和方法,将被所有实例对象共享就可以了。</p> <p>以下先介绍<code>Object</code>作为函数的用法,然后再介绍<code>Object</code>对象的原生方法,分成对象自身的方法(又称为“静态方法”)和实例方法两部分。</p> <h2 id="_2、object-当作工具方法-不通过new创建"><a href="#_2、object-当作工具方法-不通过new创建" class="header-anchor">#</a> 2、Object() 当作工具方法(不通过new创建)</h2> <p><code>Object()</code><strong>本身是一个函数</strong>,可以当作工具方法使用,将任意值转为对象。这个方法常用于保证某个值一定是对象。</p> <p>如果参数为空(或者为<code>undefined</code>和<code>null</code>),<code>Object()</code>返回一个空对象。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token function">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// obj =&gt; {}</span>
  31. <span class="token comment">// 等同于</span>
  32. obj <span class="token operator">=</span> <span class="token function">Object</span><span class="token punctuation">(</span><span class="token keyword">undefined</span><span class="token punctuation">)</span>
  33. obj <span class="token operator">=</span> <span class="token function">Object</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span>
  34. obj <span class="token keyword">instanceof</span> <span class="token class-name">Object</span> <span class="token comment">// true</span>
  35. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码的含义,是将<code>undefined</code>和<code>null</code>转为对象,结果得到了一个空对象<code>obj</code>。</p> <p><code>instanceof</code>运算符用来验证,一个对象是否为指定的构造函数的实例。<code>obj instanceof Object</code>返回<code>true</code>,就表示<code>obj</code>对象是<code>Object</code>的实例。</p> <p>如果参数是原始类型的值,<code>Object</code>方法将其转为对应的包装对象的实例(参见《原始类型的包装对象》一章)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 包装对象实例</span>
  36. <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token function">Object</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// obj =&gt; Number{1}</span>
  37. obj <span class="token keyword">instanceof</span> <span class="token class-name">Object</span> <span class="token comment">// true in s tan ce of 判断对象类型</span>
  38. obj <span class="token keyword">instanceof</span> <span class="token class-name">Number</span> <span class="token comment">// true</span>
  39. <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token function">Object</span><span class="token punctuation">(</span><span class="token string">'foo'</span><span class="token punctuation">)</span> <span class="token comment">// obj =&gt; String{‘foo’}</span>
  40. obj <span class="token keyword">instanceof</span> <span class="token class-name">Object</span> <span class="token comment">// true</span>
  41. obj <span class="token keyword">instanceof</span> <span class="token class-name">String</span> <span class="token comment">// true</span>
  42. <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token function">Object</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token comment">// obj =&gt; Boolean{‘foo’}</span>
  43. obj <span class="token keyword">instanceof</span> <span class="token class-name">Object</span> <span class="token comment">// true</span>
  44. obj <span class="token keyword">instanceof</span> <span class="token class-name">Boolean</span> <span class="token comment">// true</span>
  45. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>上面代码中,<code>Object</code>函数的参数是各种原始类型的值,转换成对象就是原始类型值对应的包装对象。</p> <p>如果<code>Object</code>方法的参数是一个对象,它总是返回该对象,即不用转换。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  46. <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token function">Object</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// obj =&gt; []</span>
  47. obj <span class="token operator">===</span> arr <span class="token comment">// true</span>
  48. <span class="token keyword">var</span> value <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  49. <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token function">Object</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span> <span class="token comment">// obj =&gt; {}</span>
  50. obj <span class="token operator">===</span> value <span class="token comment">// true</span>
  51. <span class="token keyword">var</span> <span class="token function-variable function">fn</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  52. <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token function">Object</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span> <span class="token comment">// obj =&gt; function(){}</span>
  53. obj <span class="token operator">===</span> fn <span class="token comment">// true</span>
  54. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>利用这一点,可以写一个<strong>判断变量是否为对象</strong>的函数。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>function isObject(value){
  55. return value === Object(value)
  56. }
  57. isObject([]) // true
  58. isObject({}) // true
  59. isObject('123') // false
  60. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="_3、object构造函数-通过new创建"><a href="#_3、object构造函数-通过new创建" class="header-anchor">#</a> 3、Object构造函数(通过new创建)</h2> <p><code>Object</code>构造函数的首要用途,是直接通过它来<strong>生成新对象</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 等同于 var obj = {}</span>
  61. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><blockquote><p>注意,通过<code>var obj = new Object()</code>的写法生成新对象,与字面量的写法<code>var obj = {}</code>是等价的。或者说,后者只是前者的一种简便写法。</p></blockquote> <p><code>Object</code>构造函数的用法与工具方法很相似,几乎一模一样。使用时,可以接受一个参数,如果该参数是一个对象,则直接返回这个对象;如果是一个原始类型的值,则返回该值对应的包装对象(详见《包装对象》一章)。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>var o1 = {a: 1};
  62. var o2 = new Object(o1);
  63. o1 === o2 // true
  64. var obj = new Object(123);
  65. obj instanceof Number // true
  66. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>虽然用法相似,但是<code>Object(value)</code>与<code>new Object(value)</code>两者的语义是不同的,<code>Object(value)</code>表示将<code>value</code>转成一个对象,<code>new Object(value)</code>则表示新生成一个对象,它的值是<code>value</code>。</p> <h2 id="_4、object-的静态方法"><a href="#_4、object-的静态方法" class="header-anchor">#</a> 4、Object 的静态方法</h2> <p>所谓“静态方法”,是指<strong>部署在<code>Object</code>对象自身</strong>的方法。</p> <h3 id="object-keys"><a href="#object-keys" class="header-anchor">#</a> Object.keys()</h3> <p><code>Object.keys()</code>方法和<code>Object.getOwnPropertyNames()</code>方法都用来遍历对象的属性。</p> <p><code>Object.keys()</code>方法的参数是一个对象,返回一个数组。该数组的成员都是该对象自身的(而不是继承的)所有<strong>属性名</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  67. <span class="token literal-property property">p1</span><span class="token operator">:</span> <span class="token number">123</span><span class="token punctuation">,</span>
  68. <span class="token literal-property property">p2</span><span class="token operator">:</span> <span class="token number">456</span>
  69. <span class="token punctuation">}</span>
  70. Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// ['p1','p2'] 注意是属性名</span>
  71. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="object-getownpropertynames"><a href="#object-getownpropertynames" class="header-anchor">#</a> Object.getOwnPropertyNames()</h3> <p><code>Object.getOwnPropertyNames</code>方法与<code>Object.keys</code>类似,也是接受一个对象作为参数,返回一个数组,包含了该对象自身的所有属性名。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  72. <span class="token literal-property property">p1</span><span class="token operator">:</span> <span class="token number">123</span><span class="token punctuation">,</span>
  73. <span class="token literal-property property">p2</span><span class="token operator">:</span> <span class="token number">456</span>
  74. <span class="token punctuation">}</span>
  75. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyNames</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// ['p1','p2']</span>
  76. <span class="token comment">// getOwnPropertyNames 中文含义:获取自己的属性名</span>
  77. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>对于一般的对象来说,<code>Object.keys()</code>和<code>Object.getOwnPropertyNames()</code>返回的结果是一样的。只有涉及不可枚举属性时,才会有不一样的结果。<code>Object.keys</code>方法只返回可枚举的属性(详见《对象属性的描述对象》一章),<code>Object.getOwnPropertyNames</code>方法还返回<strong>不可枚举的属性名</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'hello'</span><span class="token punctuation">,</span><span class="token string">'world'</span><span class="token punctuation">]</span>
  78. Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment">// ['0',‘1’] 如果对象是数组时返回索引字符串</span>
  79. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyNames</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment">// [‘0’,‘1’,'length']</span>
  80. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,数组的<code>length</code>属性是<strong>不可枚举的属性</strong>,所以只出现在<code>Object.getOwnPropertyNames</code>方法的返回结果中。</p> <p>由于 JavaScript 没有提供计算对象属性个数的方法,所以可以用这两个方法代替。</p> <p><strong>获取对象属性个数长度</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>var obj = {
  81. p1: 123,
  82. p2: 456
  83. };
  84. Object.keys(obj).length // 2
  85. Object.getOwnPropertyNames(obj).length // 2
  86. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>一般情况下,几乎总是使用<code>Object.keys</code>方法,遍历对象的属性。</p> <h3 id="object的其他静态方法"><a href="#object的其他静态方法" class="header-anchor">#</a> Object的其他静态方法</h3> <p>除了上面提到的两个方法,<code>Object</code>还有不少其他静态方法,将在后文逐一详细介绍。</p> <h4 id="_1-对象属性模型的相关方法"><a href="#_1-对象属性模型的相关方法" class="header-anchor">#</a> (1)对象属性模型的相关方法</h4> <ul><li><code>Object.getOwnPropertyDescriptor()</code>:获取某个属性的描述对象。</li> <li><code>Object.defineProperty()</code>:通过描述对象,定义某个属性。</li> <li><code>Object.defineProperties()</code>:通过描述对象,定义多个属性。</li></ul> <h4 id="_2-控制对象状态的方法"><a href="#_2-控制对象状态的方法" class="header-anchor">#</a> (2)控制对象状态的方法</h4> <ul><li><code>Object.preventExtensions()</code>:防止对象扩展。</li> <li><code>Object.isExtensible()</code>:判断对象是否可扩展。</li> <li><code>Object.seal()</code>:禁止对象配置。</li> <li><code>Object.isSealed()</code>:判断一个对象是否可配置。</li> <li><code>Object.freeze()</code>:冻结一个对象。</li> <li><code>Object.isFrozen()</code>:判断一个对象是否被冻结。</li></ul> <h4 id="_3-原型链相关方法"><a href="#_3-原型链相关方法" class="header-anchor">#</a> (3)原型链相关方法</h4> <ul><li><code>Object.create()</code>:该方法可以指定原型对象和属性,返回一个新的对象。</li> <li><code>Object.getPrototypeOf()</code>:获取对象的<code>Prototype</code>对象。</li></ul> <p>、</p> <p>有部分方法为列入此文档,更多方法详情:<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object" target="_blank" rel="noopener noreferrer">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h2 id="_5、object-的实例方法"><a href="#_5、object-的实例方法" class="header-anchor">#</a> 5、Object 的实例方法</h2> <p>除了静态方法,还有不少方法定义在<code>Object.prototype</code>对象。它们称为实例方法,所有<code>Object</code>的实例对象都继承了这些方法。</p> <p><code>Object</code>实例对象的方法,主要有以下六个。</p> <ul><li><code>Object.prototype.valueOf()</code>:返回当前对象对应的值。</li> <li><code>Object.prototype.toString()</code>:返回当前对象对应的字符串形式。</li> <li><code>Object.prototype.toLocaleString()</code>:返回当前对象对应的本地字符串形式。</li> <li><code>Object.prototype.hasOwnProperty()</code>:判断某个属性是否为当前对象自身的属性,还是继承自原型对象的属性。</li> <li><code>Object.prototype.isPrototypeOf()</code>:判断当前对象是否为另一个对象的原型。</li> <li><code>Object.prototype.propertyIsEnumerable()</code>:判断某个属性是否可枚举。</li></ul> <p>本节介绍前四个方法,另外两个方法将在后文相关章节介绍。</p> <h3 id="object-prototype-valueof"><a href="#object-prototype-valueof" class="header-anchor">#</a> Object.prototype.valueOf()</h3> <p><code>valueOf</code>方法的作用是返回一个对象的“值”,默认情况下<strong>返回对象本身</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  87. obj<span class="token punctuation">.</span><span class="token function">valueof</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> obj <span class="token comment">//true</span>
  88. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码比较<code>obj.valueOf()</code>与<code>obj</code>本身,两者是一样的。</p> <p><code>valueOf</code>方法的主要用途是,JavaScript <strong>自动类型转换</strong>时会默认调用这个方法(详见《数据类型转换》一章)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token function">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  89. <span class="token number">1</span><span class="token operator">+</span>obj <span class="token comment">//&quot;1[object Object]&quot;</span>
  90. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码将对象<code>obj</code>与数字<code>1</code>相加,这时 JavaScript 就会<strong>自动调用</strong><code>valueOf()</code>方法,求出<code>obj</code>的值再与<code>1</code>相加。所以,如果自定义<code>valueOf</code>方法,就可以得到想要的结果。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  91. obj<span class="token punctuation">.</span><span class="token function-variable function">valueof</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  92. <span class="token keyword">return</span> <span class="token number">2</span>
  93. <span class="token punctuation">}</span>
  94. <span class="token number">1</span> <span class="token operator">+</span> obj <span class="token comment">// 3</span>
  95. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码自定义了<code>obj</code>对象的<code>valueOf</code>方法,于是<code>1 + obj</code>就得到了<code>3</code>。这种方法就相当于用自定义的<code>obj.valueOf</code>,覆盖<code>Object.prototype.valueOf</code>。</p> <h3 id="object-prototype-tostring"><a href="#object-prototype-tostring" class="header-anchor">#</a> Object.prototype.toString()</h3> <p><code>toString</code>方法的作用是返回一个对象的字符串形式,默认情况下<strong>返回类型字符串</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> o1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  96. o1<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;[object Object]&quot;</span>
  97. <span class="token keyword">var</span> o2 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">a</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">}</span>
  98. o2<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;[object Object]&quot;</span>
  99. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码表示,对于一个对象调用<code>toString</code>方法,会返回字符串<code>[object Object]</code>,该字符串说明对象的类型。</p> <p>字符串<code>[object Object]</code>本身没有太大的用处,但是通过自定义<code>toString</code>方法,可以让对象在自动类型转换时,得到想要的字符串形式。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  100. obj<span class="token punctuation">.</span><span class="token function-variable function">toString</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  101. <span class="token keyword">return</span> <span class="token string">'hello'</span>
  102. <span class="token punctuation">}</span>
  103. obj <span class="token operator">+</span> <span class="token string">' '</span><span class="token operator">+</span> <span class="token string">'world'</span> <span class="token comment">// &quot;hello world&quot;</span>
  104. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码表示,当对象用于字符串加法时,会<strong>自动调用</strong><code>toString</code>方法。由于自定义了<code>toString</code>方法,所以返回字符串<code>hello world</code>。</p> <p>数组、字符串、函数、Date 对象都分别部署了自定义的<code>toString</code>方法,覆盖了<code>Object.prototype.toString</code>方法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1,2,3&quot;</span>
  105. <span class="token string">'123'</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;123&quot;</span>
  106. <span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  107. <span class="token keyword">return</span> <span class="token number">123</span><span class="token punctuation">;</span>
  108. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  109. <span class="token comment">// &quot;function () {</span>
  110. <span class="token comment">// return 123;</span>
  111. <span class="token comment">// }&quot;</span>
  112. <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  113. <span class="token comment">// &quot;Tue May 10 2016 09:11:31 GMT+0800 (CST)&quot;</span>
  114. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>上面代码中,数组、字符串、函数、Date 对象调用<code>toString</code>方法,并不会返回<code>[object Object]</code>,因为它们都自定义了<code>toString</code>方法,覆盖原始方法。</p> <h4 id="tostring-的应用-判断数据类型"><a href="#tostring-的应用-判断数据类型" class="header-anchor">#</a> toString() 的应用:判断数据类型</h4> <p><code>Object.prototype.toString</code>方法返回对象的类型字符串,因此可以用来判断一个值的类型。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>var obj = {};
  115. obj.toString() // &quot;[object Object]&quot;
  116. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码调用空对象的<code>toString</code>方法,结果返回一个字符串<code>object Object</code>,其中第二个<code>Object</code>表示该值的构造函数。这是一个十分有用的判断数据类型的方法。</p> <p>由于实例对象可能会自定义<code>toString</code>方法,覆盖掉<code>Object.prototype.toString</code>方法,所以为了得到类型字符串,最好直接使用<code>Object.prototype.toString</code>方法。通过函数的<code>call</code>方法,可以在任意值上调用这个方法,帮助我们判断这个值的类型。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>Object.prototype.toString.call(value)
  117. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码表示对<code>value</code>这个值调用<code>Object.prototype.toString</code>方法。</p> <p>不同数据类型的<code>Object.prototype.toString</code>方法返回值如下。</p> <ul><li>数值:返回<code>[object Number]</code>。</li> <li>字符串:返回<code>[object String]</code>。</li> <li>布尔值:返回<code>[object Boolean]</code>。</li> <li>undefined:返回<code>[object Undefined]</code>。</li> <li>null:返回<code>[object Null]</code>。</li> <li>数组:返回<code>[object Array]</code>。</li> <li>arguments 对象:返回<code>[object Arguments]</code>。</li> <li>函数:返回<code>[object Function]</code>。</li> <li>Error 对象:返回<code>[object Error]</code>。</li> <li>Date 对象:返回<code>[object Date]</code>。</li> <li>RegExp 对象:返回<code>[object RegExp]</code>。</li> <li>其他对象:返回<code>[object Object]</code>。</li></ul> <p>这就是说,<code>Object.prototype.toString</code>可以看出一个值到底是什么类型。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;[object Number]&quot;</span>
  118. <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span> <span class="token comment">// &quot;[object String]&quot;</span>
  119. <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token comment">// &quot;[object Boolean]&quot;</span>
  120. <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">undefined</span><span class="token punctuation">)</span> <span class="token comment">// &quot;[object Undefined]&quot;</span>
  121. <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token comment">// &quot;[object Null]&quot;</span>
  122. <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>Math<span class="token punctuation">)</span> <span class="token comment">// &quot;[object Math]&quot;</span>
  123. <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// &quot;[object Object]&quot;</span>
  124. <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// &quot;[object Array]&quot;</span>
  125. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>利用这个特性,可以写出一个比<code>typeof</code>运算符更准确的类型判断函数。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> <span class="token function-variable function">type</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">o</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  126. <span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span>
  127. <span class="token keyword">return</span> s<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\[object (.*?)\]</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  128. <span class="token punctuation">}</span>
  129. <span class="token function">type</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;object&quot;</span>
  130. <span class="token function">type</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;array&quot;</span>
  131. <span class="token function">type</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;number&quot;</span>
  132. <span class="token function">type</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;null&quot;</span>
  133. <span class="token function">type</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;undefined&quot;</span>
  134. <span class="token function">type</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">abcd</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;regex&quot;</span>
  135. <span class="token function">type</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;date&quot;</span>
  136. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>在上面这个<code>type</code>函数的基础上,还可以加上专门判断某种类型数据的方法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> <span class="token function-variable function">type</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">o</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  137. <span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span><span class="token punctuation">;</span>
  138. <span class="token keyword">return</span> s<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\[object (.*?)\]</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  139. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  140. <span class="token punctuation">[</span><span class="token string">'Null'</span><span class="token punctuation">,</span>
  141. <span class="token string">'Undefined'</span><span class="token punctuation">,</span>
  142. <span class="token string">'Object'</span><span class="token punctuation">,</span>
  143. <span class="token string">'Array'</span><span class="token punctuation">,</span>
  144. <span class="token string">'String'</span><span class="token punctuation">,</span>
  145. <span class="token string">'Number'</span><span class="token punctuation">,</span>
  146. <span class="token string">'Boolean'</span><span class="token punctuation">,</span>
  147. <span class="token string">'Function'</span><span class="token punctuation">,</span>
  148. <span class="token string">'RegExp'</span>
  149. <span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">t</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  150. type<span class="token punctuation">[</span><span class="token string">'is'</span> <span class="token operator">+</span> t<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">o</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  151. <span class="token keyword">return</span> <span class="token function">type</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token operator">===</span> t<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  152. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  153. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  154. type<span class="token punctuation">.</span><span class="token function">isObject</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  155. type<span class="token punctuation">.</span><span class="token function">isNumber</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  156. type<span class="token punctuation">.</span><span class="token function">isRegExp</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">abc</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  157. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h3 id="object-prototype-tolocalestring"><a href="#object-prototype-tolocalestring" class="header-anchor">#</a> Object.prototype.toLocaleString()</h3> <p><code>Object.prototype.toLocaleString</code>方法与<code>toString</code>的返回结果相同,也是返回一个值的字符串形式。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  158. obj<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// &quot;[object Object]&quot;</span>
  159. obj<span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// &quot;[object Object]&quot;</span>
  160. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>这个方法的主要作用是留出一个接口,让各种不同的对象实现自己版本的<code>toLocaleString</code>,用来返回针对某些地域的特定的值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> person <span class="token operator">=</span> <span class="token punctuation">{</span>
  161. <span class="token function-variable function">toString</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  162. <span class="token keyword">return</span> <span class="token string">'Henry Norman Bethune'</span><span class="token punctuation">;</span>
  163. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  164. <span class="token function-variable function">toLocaleString</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  165. <span class="token keyword">return</span> <span class="token string">'白求恩'</span><span class="token punctuation">;</span>
  166. <span class="token punctuation">}</span>
  167. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  168. person<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// Henry Norman Bethune</span>
  169. person<span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 白求恩</span>
  170. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,<code>toString()</code>方法返回对象的一般字符串形式,<code>toLocaleString()</code>方法返回本地的字符串形式。</p> <p>目前,主要有三个对象自定义了<code>toLocaleString</code>方法。</p> <ul><li>Array.prototype.toLocaleString()</li> <li>Number.prototype.toLocaleString()</li> <li>Date.prototype.toLocaleString()</li></ul> <p>举例来说,日期的实例对象的<code>toString</code>和<code>toLocaleString</code>返回值就不一样,而且<code>toLocaleString</code>的返回值跟用户设定的所在地域相关。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  171. date<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Sun Oct 06 2019 15:50:04 GMT+0800 (中国标准时间)&quot;</span>
  172. date<span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;2019/10/6 下午3:50:04&quot;</span>
  173. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="object-prototype-hasownproperty"><a href="#object-prototype-hasownproperty" class="header-anchor">#</a> Object.prototype.hasOwnProperty()</h3> <p><code>Object.prototype.hasOwnProperty</code>方法接受一个字符串作为参数,返回一个布尔值,表示该实例对象自身是否具有该属性。</p> <p><strong>检查对象是否包含有某个属性,返回布尔值</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>var obj = {
  174. p: 123
  175. };
  176. obj.hasOwnProperty('p') // true
  177. obj.hasOwnProperty('toString') // false
  178. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,对象<code>obj</code>自身具有<code>p</code>属性,所以返回<code>true</code>。<code>toString</code>属性是继承的,所以返回<code>false</code>。</p> <h2 id="二、属性描述对象"><a href="#二、属性描述对象" class="header-anchor">#</a> 二、属性描述对象</h2> <h2 id="_1、概述-2"><a href="#_1、概述-2" class="header-anchor">#</a> 1、概述</h2> <p>JavaScript 提供了一个内部数据结构,用来描述对象的属性,控制它的行为,比如该属性是否可写、可遍历等等。这个内部数据结构称为“属性描述对象”(attributes object)。每个属性都有自己对应的属性描述对象,保存该属性的一些元信息。</p> <p>下面是属性描述对象的一个例子。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">{</span>
  179. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">123</span><span class="token punctuation">,</span> <span class="token comment">// 该属性的属性值,默认undefined</span>
  180. <span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token comment">// 属性值是否可改变,默认为true</span>
  181. <span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token comment">// 该属性是否可遍历,默认为true</span>
  182. <span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token comment">//属性描述对象的可配置性,默认为true</span>
  183. <span class="token literal-property property">get</span><span class="token operator">:</span> <span class="token keyword">undefined</span><span class="token punctuation">,</span> <span class="token comment">// 该属性的取值函数(getter),默认为undefined</span>
  184. <span class="token literal-property property">set</span><span class="token operator">:</span> <span class="token keyword">undefined</span> <span class="token comment">// 该属性的存值函数(setter),默认为undefined</span>
  185. <span class="token punctuation">}</span>
  186. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>属性描述对象提供6个元属性。</p> <p>(1)<code>value</code></p> <p><code>value</code>是该属性的属性值,默认为<code>undefined</code>。</p> <p>(2)<code>writable</code></p> <p><code>writable</code>是一个布尔值,表示属性值(value)是否可改变(即是否可写),默认为<code>true</code>。</p> <p>(3)<code>enumerable</code></p> <p><code>enumerable</code>是一个布尔值,表示该属性是否可遍历,默认为<code>true</code>。如果设为<code>false</code>,会使得某些操作(比如<code>for...in</code>循环、<code>Object.keys()</code>)跳过该属性。</p> <p>(4)<code>configurable</code></p> <p><code>configurable</code>是一个布尔值,表示可配置性,默认为<code>true</code>。如果设为<code>false</code>,将阻止某些操作改写该属性,比如无法删除该属性,也不得改变该属性的属性描述对象(<code>value</code>属性除外)。也就是说,<code>configurable</code>属性控制了属性描述对象的可写性。</p> <p>(5)<code>get</code></p> <p><code>get</code>是一个函数,表示该属性的取值函数(getter),默认为<code>undefined</code>。</p> <p>(6)<code>set</code></p> <p><code>set</code>是一个函数,表示该属性的存值函数(setter),默认为<code>undefined</code>。</p> <h2 id="_2、object-getownpropertydescriptor"><a href="#_2、object-getownpropertydescriptor" class="header-anchor">#</a> 2、Object.getOwnPropertyDescriptor()</h2> <p><code>Object.getOwnPropertyDescriptor()</code>方法可以<strong>获取属性描述对象</strong>。它的第一个参数是目标对象,第二个参数是一个字符串,对应目标对象的某个属性名。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">p</span><span class="token operator">:</span><span class="token string">'a'</span><span class="token punctuation">}</span>
  187. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyDescriptor</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span><span class="token string">'p'</span><span class="token punctuation">)</span>
  188. <span class="token comment">// Object { value: &quot;a&quot;, // 属性值'a'</span>
  189. <span class="token comment">// writable: true, // 属性值'a' 可改写</span>
  190. <span class="token comment">// enumerable: true, // 'p'属性可被遍历</span>
  191. <span class="token comment">// configurable: true // 属性描述对象的可配置性</span>
  192. <span class="token comment">// }</span>
  193. <span class="token comment">// getOwnPropertyDescriptor 中文含义:获取自己的属性描述符</span>
  194. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>注意,<code>Object.getOwnPropertyDescriptor()</code>方法只能用于对象自身的属性,不能用于继承的属性。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token literal-property property">p</span><span class="token operator">:</span> <span class="token string">'a'</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
  195. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyDescriptor</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'toString'</span><span class="token punctuation">)</span>
  196. <span class="token comment">// undefined</span>
  197. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,<code>toString</code>是<code>obj</code>对象继承的属性,<code>Object.getOwnPropertyDescriptor()</code>无法获取。</p> <h2 id="_3、object-getownpropertynames"><a href="#_3、object-getownpropertynames" class="header-anchor">#</a> 3、Object.getOwnPropertyNames()</h2> <p><code>Object.getOwnPropertyNames</code>方法返回一个数组,成员是参数对象自身的全部属性的属性名,不管该属性是否可遍历。</p> <p><strong>返回对象的全部属性名</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperties</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  198. <span class="token literal-property property">p1</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  199. <span class="token literal-property property">p2</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">false</span> <span class="token punctuation">}</span>
  200. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  201. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyNames</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
  202. <span class="token comment">// [&quot;p1&quot;, &quot;p2&quot;]</span>
  203. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中,<code>obj.p1</code>是可遍历的,<code>obj.p2</code>是不可遍历的。<code>Object.getOwnPropertyNames</code>会将它们都返回。</p> <p>这跟<code>Object.keys</code>的行为不同,<code>Object.keys</code>只返回对象自身的可遍历属性的全部属性名。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// []</span>
  204. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyNames</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// [ 'length' ]</span>
  205. Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">)</span> <span class="token comment">// []</span>
  206. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyNames</span><span class="token punctuation">(</span><span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">)</span>
  207. <span class="token comment">/*[
  208. &quot;constructor&quot;, // 构造函数
  209. &quot;__defineGetter__&quot;,
  210. &quot;__defineSetter__&quot;,
  211. &quot;hasOwnProperty&quot;, // 检测属性名
  212. &quot;__lookupGetter__&quot;,
  213. &quot;__lookupSetter__&quot;,
  214. &quot;isPrototypeOf&quot;,
  215. &quot;propertyIsEnumerable&quot;,
  216. &quot;toString&quot;, // 返回类型字符串
  217. &quot;valueOf&quot;,
  218. &quot;__proto__&quot;, // 原型链
  219. &quot;toLocaleString&quot;
  220. ]
  221. */</span>
  222. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p>上面代码中,数组自身的<code>length</code>属性是不可遍历的,<code>Object.keys</code>不会返回该属性。第二个例子的<code>Object.prototype</code>也是一个对象,所有实例对象都会继承它,它自身的属性都是不可遍历的。</p> <h2 id="_4、object-defineproperty-object-defineproperties"><a href="#_4、object-defineproperty-object-defineproperties" class="header-anchor">#</a> 4、Object.defineProperty(),Object.defineProperties()</h2> <p><code>Object.defineProperty()</code>方法允许通过属性描述对象,定义或修改一个属性,然后返回修改后的对象,它的用法如下。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>object<span class="token punctuation">,</span> propertyName<span class="token punctuation">,</span> attributesObject<span class="token punctuation">)</span>
  223. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>Object.defineProperty</code>方法接受三个参数,依次如下。</p> <ul><li>object:属性所在的对象</li> <li>propertyName:字符串,表示属性名</li> <li>attributesObject:属性描述对象</li></ul> <p>举例来说,定义<code>obj.p</code>可以写成下面这样。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  224. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">123</span><span class="token punctuation">,</span>
  225. <span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token comment">// 设置为false,无法被改写</span>
  226. <span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token comment">// 可遍历</span>
  227. <span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">false</span> <span class="token comment">// 属性描述不可再被配置</span>
  228. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  229. obj<span class="token punctuation">.</span>p <span class="token comment">// 123</span>
  230. obj<span class="token punctuation">.</span>p <span class="token operator">=</span> <span class="token number">246</span><span class="token punctuation">;</span> <span class="token comment">// 由于writable为false,改写无效</span>
  231. obj<span class="token punctuation">.</span>p <span class="token comment">// 123</span>
  232. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,<code>Object.defineProperty()</code>方法定义了<code>obj.p</code>属性。由于属性描述对象的<code>writable</code>属性为<code>false</code>,所以<code>obj.p</code>属性不可写。注意,这里的<code>Object.defineProperty</code>方法的第一个参数是<code>{}</code>(一个新建的空对象),<code>p</code>属性直接定义在这个空对象上面,然后返回这个对象,这是<code>Object.defineProperty()</code>的常见用法。</p> <p>如果属性已经存在,<code>Object.defineProperty()</code>方法相当于更新该属性的属性描述对象。</p> <p>如果<strong>一次性定义或修改多个属性</strong>,可以使用<code>Object.defineProperties()</code>方法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperties</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  233. <span class="token literal-property property">p1</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">123</span><span class="token punctuation">,</span> <span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  234. <span class="token literal-property property">p2</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token string">'abc'</span><span class="token punctuation">,</span> <span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  235. <span class="token literal-property property">p3</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token function-variable function">get</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>p1 <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>p2 <span class="token punctuation">}</span><span class="token punctuation">,</span>
  236. <span class="token literal-property property">enumerable</span><span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span>
  237. <span class="token literal-property property">configurable</span><span class="token operator">:</span><span class="token boolean">true</span>
  238. <span class="token punctuation">}</span>
  239. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  240. obj<span class="token punctuation">.</span>p1 <span class="token comment">// 123</span>
  241. obj<span class="token punctuation">.</span>p2 <span class="token comment">// &quot;abc&quot;</span>
  242. obj<span class="token punctuation">.</span>p3 <span class="token comment">// &quot;123abc&quot;</span>
  243. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>上面代码中,<code>Object.defineProperties()</code>同时定义了<code>obj</code>对象的三个属性。其中,<code>p3</code>属性定义了取值函数<code>get</code>,即每次读取该属性,都会调用这个取值函数。</p> <p><strong>注意</strong>,一旦定义了取值函数<code>get</code>(或存值函数<code>set</code>),就不能将<code>writable</code>属性设为<code>true</code>,或者同时定义<code>value</code>属性,否则会报错。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  244. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  245. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">123</span><span class="token punctuation">,</span>
  246. <span class="token function-variable function">get</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">456</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
  247. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  248. <span class="token comment">// TypeError: Invalid property.</span>
  249. <span class="token comment">// A property cannot both have accessors and be writable or have a value</span>
  250. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  251. <span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
  252. <span class="token function-variable function">get</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">456</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
  253. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  254. <span class="token comment">// TypeError: Invalid property descriptor.</span>
  255. <span class="token comment">// Cannot both specify accessors and a value or writable attribute</span>
  256. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>上面代码中,同时定义了<code>get</code>属性和<code>value</code>属性,以及将<code>writable</code>属性设为<code>true</code>,就会报错。</p> <p><code>Object.defineProperty()</code>和<code>Object.defineProperties()</code>参数里面的属性描述对象,<code>writable</code>、<code>configurable</code>、<code>enumerable</code>这三个属性的默认值都为<code>false</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  257. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  258. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyDescriptor</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">)</span>
  259. <span class="token comment">// { // 默认值</span>
  260. <span class="token comment">// value: undefined,</span>
  261. <span class="token comment">// writable: false,</span>
  262. <span class="token comment">// enumerable: false,</span>
  263. <span class="token comment">// configurable: false</span>
  264. <span class="token comment">// }</span>
  265. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,定义<code>obj.foo</code>时用了一个空的属性描述对象,就可以看到各个元属性的默认值。</p> <h2 id="_5、object-prototype-propertyisenumerable"><a href="#_5、object-prototype-propertyisenumerable" class="header-anchor">#</a> 5、Object.prototype.propertyIsEnumerable()</h2> <p>实例对象的<code>propertyIsEnumerable()</code>方法返回一个布尔值,用来<strong>判断某个属性是否可遍历</strong>。注意,这个方法只能用于判断对象自身的属性,对于继承的属性一律返回<code>false</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  266. obj<span class="token punctuation">.</span>p <span class="token operator">=</span> <span class="token number">123</span><span class="token punctuation">;</span>
  267. obj<span class="token punctuation">.</span><span class="token function">propertyIsEnumerable</span><span class="token punctuation">(</span><span class="token string">'p'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  268. obj<span class="token punctuation">.</span><span class="token function">propertyIsEnumerable</span><span class="token punctuation">(</span><span class="token string">'toString'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  269. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,<code>obj.p</code>是可遍历的,而<code>obj.toString</code>是继承的属性。</p> <h2 id="_6、元属性"><a href="#_6、元属性" class="header-anchor">#</a> 6、元属性</h2> <p>属性描述对象的各个属性称为“元属性”,因为它们可以看作是控制属性的属性。</p> <h3 id="value目标属性的值"><a href="#value目标属性的值" class="header-anchor">#</a> value目标属性的值</h3> <p><code>value</code>属性是目标属性的值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  270. obj<span class="token punctuation">.</span>p <span class="token operator">=</span> <span class="token number">123</span><span class="token punctuation">;</span>
  271. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyDescriptor</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>value
  272. <span class="token comment">// 123</span>
  273. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">246</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  274. obj<span class="token punctuation">.</span>p <span class="token comment">// 246</span>
  275. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码是通过<code>value</code>属性,读取或改写<code>obj.p</code>的例子。</p> <h3 id="writable可写性"><a href="#writable可写性" class="header-anchor">#</a> writable可写性</h3> <p><code>writable</code>属性是一个布尔值,决定了目标属性的值(value)是否可以被改变。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  276. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  277. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">37</span><span class="token punctuation">,</span>
  278. <span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">false</span>
  279. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  280. obj<span class="token punctuation">.</span>a <span class="token comment">// 37</span>
  281. obj<span class="token punctuation">.</span>a <span class="token operator">=</span> <span class="token number">25</span><span class="token punctuation">;</span> <span class="token comment">// 可写性为false,值无法被修改</span>
  282. obj<span class="token punctuation">.</span>a <span class="token comment">// 37</span>
  283. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>上面代码中,<code>obj.a</code>的<code>writable</code>属性是<code>false</code>。然后,改变<code>obj.a</code>的值,不会有任何效果。</p> <p><strong>注意</strong>,正常模式下,对<code>writable</code>为<code>false</code>的属性赋值不会报错,只会默默失败。但是,严格模式下会报错,即使对<code>a</code>属性重新赋予一个同样的值。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>'use strict';
  284. var obj = {};
  285. Object.defineProperty(obj, 'a', {
  286. value: 37,
  287. writable: false
  288. });
  289. obj.a = 37;
  290. // Uncaught TypeError: Cannot assign to read only property 'a' of object
  291. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>上面代码是严格模式,对<code>obj.a</code>任何赋值行为都会报错。</p> <p>如果原型对象的某个属性的<code>writable</code>为<code>false</code>,那么子对象将无法自定义这个属性。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> proto <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  292. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token string">'a'</span><span class="token punctuation">,</span>
  293. <span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">false</span>
  294. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  295. <span class="token keyword">var</span> obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>proto<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// obj对象继承proto对象</span>
  296. obj<span class="token punctuation">.</span>foo <span class="token operator">=</span> <span class="token string">'b'</span><span class="token punctuation">;</span>
  297. obj<span class="token punctuation">.</span>foo <span class="token comment">// 'a'</span>
  298. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,<code>proto</code>是原型对象,它的<code>foo</code>属性不可写。<code>obj</code>对象继承<code>proto</code>,也不可以再自定义这个属性了。如果是严格模式,这样做还会抛出一个错误。</p> <p>但是,有一个规避方法,就是通过覆盖属性描述对象,绕过这个限制。原因是这种情况下,原型链会被完全忽视。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> proto <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  299. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token string">'a'</span><span class="token punctuation">,</span>
  300. <span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">false</span>
  301. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  302. <span class="token keyword">var</span> obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>proto<span class="token punctuation">)</span><span class="token punctuation">;</span>
  303. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  304. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token string">'b'</span>
  305. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  306. obj<span class="token punctuation">.</span>foo <span class="token comment">// &quot;b&quot;</span>
  307. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="enumerable可遍历性"><a href="#enumerable可遍历性" class="header-anchor">#</a> enumerable可遍历性</h3> <p><code>enumerable</code>(可遍历性)返回一个布尔值,表示目标属性是否可遍历。</p> <p>JavaScript 的早期版本,<code>for...in</code>循环是基于<code>in</code>运算符的。我们知道,<code>in</code>运算符不管某个属性是对象自身的还是继承的,都会返回<code>true</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  308. <span class="token string">'toString'</span> <span class="token keyword">in</span> obj <span class="token comment">// true</span>
  309. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,<code>toString</code>不是<code>obj</code>对象自身的属性,但是<code>in</code>运算符也返回<code>true</code>,这导致了<code>toString</code>属性也会被<code>for...in</code>循环遍历。</p> <p>这显然不太合理,后来就引入了“可遍历性”这个概念。只有可遍历的属性,才会被<code>for...in</code>循环遍历,同时还规定<code>toString</code>这一类实例对象继承的原生属性,都是不可遍历的,这样就保证了<code>for...in</code>循环的可用性。</p> <p>具体来说,如果一个属性的<code>enumerable</code>为<code>false</code>,下面三个操作不会取到该属性。</p> <ul><li><code>for..in</code>循环</li> <li><code>Object.keys</code>方法</li> <li><code>JSON.stringify</code>方法</li></ul> <p>因此,<code>enumerable</code>可以用来设置“秘密”属性。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  310. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'x'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  311. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">123</span><span class="token punctuation">,</span>
  312. <span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">false</span>
  313. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  314. obj<span class="token punctuation">.</span>x <span class="token comment">// 123</span>
  315. <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  316. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
  317. <span class="token punctuation">}</span>
  318. <span class="token comment">// undefined</span>
  319. Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// []</span>
  320. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// &quot;{}&quot;</span>
  321. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>上面代码中,<code>obj.x</code>属性的<code>enumerable</code>为<code>false</code>,所以一般的遍历操作都无法获取该属性,使得它有点像“秘密”属性,但不是真正的私有属性,还是可以直接获取它的值。</p> <p>注意,<code>for...in</code>循环包括继承的属性,<code>Object.keys</code>方法不包括继承的属性。如果需要获取对象自身的所有属性,不管是否可遍历,可以使用<code>Object.getOwnPropertyNames</code>方法。</p> <p>另外,<code>JSON.stringify</code>方法会排除<code>enumerable</code>为<code>false</code>的属性,有时<strong>可以利用这一点。如果对象的 JSON 格式输出要排除某些属性,就可以把这些属性的<code>enumerable</code>设为<code>false</code>。</strong></p> <h3 id="configurable可配置性"><a href="#configurable可配置性" class="header-anchor">#</a> configurable可配置性</h3> <p><code>configurable</code>(可配置性)返回一个布尔值,决定了<strong>是否可以修改属性描述对象</strong>。也就是说,<code>configurable</code>为<code>false</code>时,<code>value</code>、<code>writable</code>、<code>enumerable</code>和<code>configurable</code>都不能被修改了。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  322. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
  323. <span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
  324. <span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
  325. <span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">false</span>
  326. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  327. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  328. <span class="token comment">// TypeError: Cannot redefine property: p</span>
  329. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  330. <span class="token comment">// TypeError: Cannot redefine property: p</span>
  331. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  332. <span class="token comment">// TypeError: Cannot redefine property: p</span>
  333. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  334. <span class="token comment">// TypeError: Cannot redefine property: p</span>
  335. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>上面代码中,<code>obj.p</code>的<code>configurable</code>为<code>false</code>。然后,改动<code>value</code>、<code>writable</code>、<code>enumerable</code>、<code>configurable</code>,结果都报错。</p> <p><strong>注意</strong>,<code>writable</code>只有在<code>false</code>改为<code>true</code>会报错,<code>true</code>改为<code>false</code>是允许的。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  336. <span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
  337. <span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">false</span>
  338. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  339. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  340. <span class="token comment">// 修改成功</span>
  341. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>至于<code>value</code>,只要<code>writable</code>和<code>configurable</code>有一个为<code>true</code>,就允许改动。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> o1 <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  342. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
  343. <span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
  344. <span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">false</span>
  345. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  346. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>o1<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  347. <span class="token comment">// 修改成功</span>
  348. <span class="token keyword">var</span> o2 <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  349. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
  350. <span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
  351. <span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">true</span>
  352. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  353. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>o2<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  354. <span class="token comment">// 修改成功</span>
  355. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>另外,<code>writable</code>为<code>false</code>时,直接给目标属性赋值,不报错,但不会成功。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  356. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
  357. <span class="token literal-property property">writable</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
  358. <span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">false</span>
  359. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  360. obj<span class="token punctuation">.</span>p <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
  361. obj<span class="token punctuation">.</span>p <span class="token comment">// 1</span>
  362. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码中,<code>obj.p</code>的<code>writable</code>为<code>false</code>,对<code>obj.p</code>直接赋值不会生效。如果是严格模式,还会报错。</p> <p>可配置性决定了目标属性是否可以被删除(delete)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperties</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  363. <span class="token literal-property property">p1</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  364. <span class="token literal-property property">p2</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token literal-property property">configurable</span><span class="token operator">:</span> <span class="token boolean">false</span> <span class="token punctuation">}</span>
  365. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  366. <span class="token keyword">delete</span> obj<span class="token punctuation">.</span>p1 <span class="token comment">// true</span>
  367. <span class="token keyword">delete</span> obj<span class="token punctuation">.</span>p2 <span class="token comment">// false</span>
  368. obj<span class="token punctuation">.</span>p1 <span class="token comment">// undefined</span>
  369. obj<span class="token punctuation">.</span>p2 <span class="token comment">// 2</span>
  370. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>上面代码中,<code>obj.p1</code>的<code>configurable</code>是<code>true</code>,所以可以被删除,<code>obj.p2</code>就无法删除。</p> <h2 id="_7、存取器-get和set"><a href="#_7、存取器-get和set" class="header-anchor">#</a> 7、存取器(get和set)</h2> <p>除了直接定义以外,属性还可以用存取器(accessor)定义。其中,存值函数称为<code>setter</code>,使用属性描述对象的<code>set</code>属性;取值函数称为<code>getter</code>,使用属性描述对象的<code>get</code>属性。</p> <p><strong>一旦对目标属性定义了存取器,那么存取的时候,都将执行对应的函数</strong>。利用这个功能,可以实现许多高级特性,比如某个属性禁止赋值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  371. <span class="token function-variable function">get</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 获取属性时执行</span>
  372. <span class="token keyword">return</span> <span class="token string">'getter'</span> <span class="token comment">// 这里相当于定义了p属性的值,如果设置了get则不能再设置value</span>
  373. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  374. <span class="token function-variable function">set</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 设置属性时执行</span>
  375. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'setter: '</span> <span class="token operator">+</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
  376. <span class="token punctuation">}</span>
  377. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  378. obj<span class="token punctuation">.</span>p <span class="token comment">// &quot;getter&quot;</span>
  379. obj<span class="token punctuation">.</span>p <span class="token operator">=</span> <span class="token number">123</span> <span class="token comment">// &quot;setter: 123&quot;</span>
  380. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,<code>obj.p</code>定义了<code>get</code>和<code>set</code>属性。<code>obj.p</code>取值时,就会调用<code>get</code>;赋值时,就会调用<code>set</code>。</p> <p>JavaScript 还提供了存取器的<strong>另一种写法</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  381. <span class="token keyword">get</span> <span class="token function">p</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">// 相当于为obj对象创建一个p属性,并且赋值为‘getter’</span>
  382. <span class="token keyword">return</span> <span class="token string">'getter'</span>
  383. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  384. ser <span class="token function">p</span><span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  385. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'setter'</span><span class="token operator">+</span>value<span class="token punctuation">)</span>
  386. <span class="token punctuation">}</span>
  387. <span class="token punctuation">}</span>
  388. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面的写法与定义属性描述对象是等价的,而且使用更广泛。</p> <p>注意,取值函数<code>get</code>不能接受参数,存值函数<code>set</code>只能接受一个参数(即属性的值)。</p> <p>存取器往往用于,属性的值依赖对象内部数据的场合。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span><span class="token punctuation">{</span>
  389. <span class="token literal-property property">$n</span> <span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
  390. <span class="token keyword">get</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$n<span class="token operator">++</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  391. <span class="token keyword">set</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  392. <span class="token keyword">if</span> <span class="token punctuation">(</span>n <span class="token operator">&gt;=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$n<span class="token punctuation">)</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$n <span class="token operator">=</span> n<span class="token punctuation">;</span>
  393. <span class="token keyword">else</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">'新的值必须大于当前值'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  394. <span class="token punctuation">}</span>
  395. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  396. obj<span class="token punctuation">.</span>next <span class="token comment">// 5</span>
  397. obj<span class="token punctuation">.</span>next <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
  398. obj<span class="token punctuation">.</span>next <span class="token comment">// 10</span>
  399. obj<span class="token punctuation">.</span>next <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
  400. <span class="token comment">// Uncaught Error: 新的值必须大于当前值</span>
  401. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>上面代码中,<code>next</code>属性的存值函数和取值函数,都依赖于内部属性<code>$n</code>。</p> <blockquote><p>vue框架就是通过存取器来实现的数据响应式</p></blockquote> <h2 id="_8、对象的拷贝"><a href="#_8、对象的拷贝" class="header-anchor">#</a> 8、对象的拷贝</h2> <p>有时,我们需要将一个对象的所有属性,拷贝到另一个对象,可以用下面的方法实现。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> <span class="token function-variable function">extend</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> <span class="token keyword">from</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  402. <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> property <span class="token keyword">in</span> from<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  403. to<span class="token punctuation">[</span>property<span class="token punctuation">]</span> <span class="token operator">=</span> from<span class="token punctuation">[</span>property<span class="token punctuation">]</span>
  404. <span class="token punctuation">}</span>
  405. <span class="token keyword">return</span> to
  406. <span class="token punctuation">}</span>
  407. <span class="token function">extend</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// {a:1}</span>
  408. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面这个方法的问题在于,如果遇到存取器定义的属性,会只拷贝到属性和值,并不能拷贝到存取器</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>extend({}, {
  409. get a() { return 1 }
  410. })
  411. // {a: 1} // 这里并没有拷贝到get方法
  412. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>为了解决这个问题,我们可以通过<code>Object.defineProperty</code>方法来拷贝属性。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> <span class="token function-variable function">extend</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> <span class="token keyword">from</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  413. <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> property <span class="token keyword">in</span> from<span class="token punctuation">)</span><span class="token punctuation">{</span>
  414. <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>from<span class="token punctuation">.</span><span class="token function">hasOwnProperty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">continue</span> <span class="token comment">//过滤继承属性 continue 终止本次循环,继续剩余循环</span>
  415. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>
  416. to<span class="token punctuation">,</span>
  417. property<span class="token punctuation">,</span>
  418. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyDescriptor</span><span class="token punctuation">(</span>from<span class="token punctuation">,</span>property<span class="token punctuation">)</span>
  419. <span class="token punctuation">)</span>
  420. <span class="token punctuation">}</span>
  421. <span class="token keyword">return</span> to
  422. <span class="token punctuation">}</span>
  423. <span class="token function">extend</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token keyword">get</span> <span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// {get a(){return 1}}</span>
  424. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>上面代码中,<code>hasOwnProperty</code>那一行用来过滤掉继承的属性,否则可能会报错,因为<code>Object.getOwnPropertyDescriptor</code>读不到继承属性的属性描述对象。</p> <h2 id="_9、控制对象读写状态"><a href="#_9、控制对象读写状态" class="header-anchor">#</a> 9、控制对象读写状态</h2> <p>有时需要<strong>冻结对象的读写状态,防止对象被改变</strong>。</p> <p>JavaScript 提供了三种冻结方法:</p> <ul><li><p>最弱的一种是<code>Object.preventExtensions</code> (不能新增,但能被删除和修改)</p></li> <li><p>其次是<code>Object.seal</code> (不能新增和删除,但能修改)</p></li> <li><p>最强的是<code>Object.freeze</code> (不能新增、删除和修改)</p></li></ul> <h3 id="object-preventextensions-防止新增"><a href="#object-preventextensions-防止新增" class="header-anchor">#</a> Object.preventExtensions() 防止新增</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  425. Object<span class="token punctuation">.</span><span class="token function">preventExtensions</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// prevent Extensions 中文: 防止扩展</span>
  426. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span><span class="token string">'p'</span><span class="token punctuation">,</span><span class="token punctuation">{</span>
  427. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token string">'hello'</span>
  428. <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// TypeError:cannot define property:p, object is not extensible</span>
  429. obj<span class="token punctuation">.</span>p <span class="token operator">=</span> <span class="token number">1</span>
  430. obj<span class="token punctuation">.</span>p <span class="token comment">// undefined</span>
  431. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码中,<code>obj</code>对象经过<code>Object.preventExtensions</code>以后,就无法添加新属性了。</p> <h3 id="object-isextensible-是可新增的"><a href="#object-isextensible-是可新增的" class="header-anchor">#</a> Object.isExtensible() 是可新增的?</h3> <p><code>Object.isExtensible</code>方法用于<strong>检查一个对象是否使用了<code>Object.preventExtensions</code>方法</strong>。也就是说,检查是否可以为一个对象添加属性。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  432. Object<span class="token punctuation">.</span><span class="token function">isExtensible</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// true is Extensible 中文: 是可扩展的?</span>
  433. Object<span class="token punctuation">.</span><span class="token function">preventExtensions</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
  434. Object<span class="token punctuation">.</span><span class="token function">isExtensible</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// false</span>
  435. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,对<code>obj</code>对象使用<code>Object.preventExtensions</code>方法以后,再使用<code>Object.isExtensible</code>方法,返回<code>false</code>,表示已经不能添加新属性了。</p> <h3 id="object-seal-密封"><a href="#object-seal-密封" class="header-anchor">#</a> Object.seal() 密封</h3> <p><code>Object.seal</code>方法使得一个对象<strong>既无法添加新属性,也无法删除旧属性。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">p</span><span class="token operator">:</span> <span class="token string">'hello'</span><span class="token punctuation">}</span>
  436. Object<span class="token punctuation">.</span><span class="token function">seal</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// seal 中文: 密封</span>
  437. <span class="token keyword">delete</span> obj<span class="token punctuation">.</span>p <span class="token comment">// 删除属性无效</span>
  438. obj<span class="token punctuation">.</span>p <span class="token comment">// hello</span>
  439. obj<span class="token punctuation">.</span>p <span class="token operator">=</span> <span class="token string">'hi'</span> <span class="token comment">// 可修改值</span>
  440. obj<span class="token punctuation">.</span>p <span class="token comment">// hi</span>
  441. obj<span class="token punctuation">.</span>x <span class="token operator">=</span> <span class="token string">'world'</span> <span class="token comment">// 新增属性无效</span>
  442. obj<span class="token punctuation">.</span>x <span class="token comment">// undefined</span>
  443. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,<code>obj</code>对象执行<code>Object.seal</code>方法以后,就无法添加新属性和删除旧属性了。</p> <p><code>Object.seal</code>实质是把属性描述对象的<code>configurable</code>属性设为<code>false</code>,因此属性描述对象不再能改变了。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">p</span><span class="token operator">:</span> <span class="token string">'a'</span><span class="token punctuation">}</span>
  444. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyDescriptor</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">)</span>
  445. <span class="token comment">// Object {</span>
  446. <span class="token comment">// value: &quot;a&quot;,</span>
  447. <span class="token comment">// writable: true,</span>
  448. <span class="token comment">// enumerable: true,</span>
  449. <span class="token comment">// configurable: true</span>
  450. <span class="token comment">// }</span>
  451. Object<span class="token punctuation">.</span><span class="token function">seal</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
  452. Object<span class="token punctuation">.</span><span class="token function">getOwnPropertyDescriptor</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'p'</span><span class="token punctuation">)</span>
  453. <span class="token comment">// Object {</span>
  454. <span class="token comment">// value: &quot;a&quot;,</span>
  455. <span class="token comment">// writable: true,</span>
  456. <span class="token comment">// enumerable: true,</span>
  457. <span class="token comment">// configurable: false</span>
  458. <span class="token comment">// }</span>
  459. Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span><span class="token string">'p'</span><span class="token punctuation">,</span><span class="token punctuation">{</span>
  460. <span class="token literal-property property">enumerable</span><span class="token operator">:</span>
  461. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  462. <span class="token comment">// TypeError: Cannot redefine property: p</span>
  463. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><p>上面代码中,使用<code>Object.seal</code>方法之后,属性描述对象的<code>configurable</code>属性就变成了<code>false</code>,然后改变<code>enumerable</code>属性就会报错。</p> <p><code>Object.seal</code>只是禁止新增或删除属性,并不影响修改某个属性的值。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>var obj = { p: 'a' };
  464. Object.seal(obj);
  465. obj.p = 'b';
  466. obj.p // 'b'
  467. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,<code>Object.seal</code>方法对<code>p</code>属性的<code>value</code>无效,是因为此时<code>p</code>属性的可写性由<code>writable</code>决定。</p> <h3 id="object-issealed-是密封的"><a href="#object-issealed-是密封的" class="header-anchor">#</a> Object.isSealed() 是密封的?</h3> <p><code>Object.isSealed</code>方法用于<strong>检查一个对象是否使用了<code>Object.seal</code>方法</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">p</span><span class="token operator">:</span><span class="token string">'a'</span><span class="token punctuation">}</span>
  468. Object<span class="token punctuation">.</span><span class="token function">seal</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
  469. Object<span class="token punctuation">.</span><span class="token function">isSealed</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// true is Sealed 中文: 是密封的?</span>
  470. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>这时,<code>Object.isExtensible</code>方法也返回<code>false</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token literal-property property">p</span><span class="token operator">:</span> <span class="token string">'a'</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
  471. Object<span class="token punctuation">.</span><span class="token function">seal</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
  472. Object<span class="token punctuation">.</span><span class="token function">isExtensible</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// false</span>
  473. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><blockquote><p>isSealed为true,是密封的。isExtensible为false,不是可扩展的</p></blockquote> <h3 id="object-freeze-冻结"><a href="#object-freeze-冻结" class="header-anchor">#</a> Object.freeze() 冻结</h3> <p><code>Object.freeze</code>方法可以使得一个对象<strong>无法添加新属性、无法删除旧属性、也无法改变属性的值</strong>,使得这个对象实际上变成了常量。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">p</span><span class="token operator">:</span><span class="token string">'hello'</span><span class="token punctuation">}</span>
  474. Object<span class="token punctuation">.</span><span class="token function">freeze</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
  475. obj<span class="token punctuation">.</span>p <span class="token operator">=</span> <span class="token string">'world'</span>
  476. obj<span class="token punctuation">.</span>p <span class="token comment">// hello</span>
  477. obj<span class="token punctuation">.</span>t <span class="token operator">=</span> <span class="token string">'hello'</span>
  478. obj<span class="token punctuation">.</span>t <span class="token comment">// nudefined</span>
  479. <span class="token keyword">delete</span> obj<span class="token punctuation">.</span>p <span class="token comment">// false</span>
  480. obj<span class="token punctuation">.</span>p <span class="token comment">// hello</span>
  481. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,对<code>obj</code>对象进行<code>Object.freeze()</code>以后,修改属性、新增属性、删除属性都无效了。这些操作并不报错,只是默默地失败。如果在严格模式下,则会报错。</p> <h3 id="object-isfrozen-是冻结的"><a href="#object-isfrozen-是冻结的" class="header-anchor">#</a> Object.isFrozen() 是冻结的?</h3> <p><code>Object.isFrozen</code>方法用于<strong>检查一个对象是否使用了<code>Object.freeze</code>方法。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  482. <span class="token literal-property property">p</span><span class="token operator">:</span> <span class="token string">'hello'</span>
  483. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  484. Object<span class="token punctuation">.</span><span class="token function">freeze</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
  485. Object<span class="token punctuation">.</span><span class="token function">isFrozen</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// true // 是冻结的</span>
  486. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>使用<code>Object.freeze</code>方法以后,<code>Object.isSealed</code>将会返回<code>true</code>,<code>Object.isExtensible</code>返回<code>false</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  487. <span class="token literal-property property">p</span><span class="token operator">:</span> <span class="token string">'hello'</span>
  488. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  489. Object<span class="token punctuation">.</span><span class="token function">freeze</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
  490. Object<span class="token punctuation">.</span><span class="token function">isSealed</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// true // 是密封的</span>
  491. Object<span class="token punctuation">.</span><span class="token function">isExtensible</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// false // 不是可扩展的</span>
  492. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><code>Object.isFrozen</code>的一个<strong>用途</strong>是,确认某个对象没有被冻结后,再对它的属性赋值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  493. <span class="token literal-property property">p</span><span class="token operator">:</span> <span class="token string">'hello'</span>
  494. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  495. Object<span class="token punctuation">.</span><span class="token function">freeze</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
  496. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>Object<span class="token punctuation">.</span><span class="token function">isFrozen</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  497. obj<span class="token punctuation">.</span>p <span class="token operator">=</span> <span class="token string">'world'</span><span class="token punctuation">;</span>
  498. <span class="token punctuation">}</span>
  499. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,确认<code>obj</code>没有被冻结后,再对它的属性赋值,就不会报错了。</p> <h3 id="局限性"><a href="#局限性" class="header-anchor">#</a> 局限性</h3> <p>上面的三个方法锁定对象的可写性有一个<strong>漏洞</strong>:<strong>可以通过改变原型对象,来为对象增加属性</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  500. Object<span class="token punctuation">.</span><span class="token function">preventExtensions</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
  501. <span class="token keyword">var</span> proto <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">getPrototypeOf</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 获取原型对象</span>
  502. proto<span class="token punctuation">.</span>t <span class="token operator">=</span> <span class="token string">'hello'</span><span class="token punctuation">;</span>
  503. obj<span class="token punctuation">.</span>t
  504. <span class="token comment">// hello</span>
  505. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中,对象<code>obj</code>本身不能新增属性,但是可以在它的原型对象上新增属性,就依然能够在<code>obj</code>上读到。</p> <p>一种解决方案是,把<code>obj</code>的原型也冻结住。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  506. Object<span class="token punctuation">.</span><span class="token function">preventExtensions</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
  507. <span class="token keyword">var</span> proto <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">getPrototypeOf</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
  508. Object<span class="token punctuation">.</span><span class="token function">preventExtensions</span><span class="token punctuation">(</span>proto<span class="token punctuation">)</span><span class="token punctuation">;</span>
  509. proto<span class="token punctuation">.</span>t <span class="token operator">=</span> <span class="token string">'hello'</span><span class="token punctuation">;</span>
  510. obj<span class="token punctuation">.</span>t <span class="token comment">// undefined</span>
  511. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><strong>另外一个局限</strong>是,如果属性值是对象,上面这些方法只能冻结属性指向的对象,而不能冻结对象本身的内容。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  512. <span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
  513. <span class="token literal-property property">bar</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">]</span>
  514. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  515. Object<span class="token punctuation">.</span><span class="token function">freeze</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
  516. obj<span class="token punctuation">.</span>bar<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'c'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  517. obj<span class="token punctuation">.</span>bar <span class="token comment">// [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
  518. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码中,<code>obj.bar</code>属性指向一个数组,<code>obj</code>对象被冻结以后,这个指向无法改变,即无法指向其他值,但是所指向的数组是可以改变的。</p> <h2 id="三、array对象"><a href="#三、array对象" class="header-anchor">#</a> 三、Array对象</h2> <h2 id="_1、array构造函数"><a href="#_1、array构造函数" class="header-anchor">#</a> 1、Array构造函数</h2> <p><code>Array</code>是 JavaScript 的原生对象,同时也是一个构造函数,可以用它生成新的数组。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Array是原生对象,也是构造函数; 2表示生成2个成员</span>
  519. arr<span class="token punctuation">.</span>length <span class="token comment">// 2</span>
  520. arr <span class="token comment">// [ empty x 2 ]</span>
  521. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,<code>Array</code>构造函数的参数<code>2</code>,表示生成一个两个成员的数组,每个位置都是空值。</p> <p>如果没有使用<code>new</code>,运行结果也是一样的。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  522. <span class="token comment">// 等同于</span>
  523. <span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token function">Array</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  524. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>Array</code>构造函数有一个很大的<strong>缺陷</strong>,就是不同的参数,会导致它的行为不一致。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 无参数时,返回一个空数组</span>
  525. <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// []</span>
  526. <span class="token comment">// 单个正整数参数,表示返回的新数组的长度</span>
  527. <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// [ empty ]</span>
  528. <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// [ empty x 2 ]</span>
  529. <span class="token comment">// 非正整数的数值作为参数,会报错</span>
  530. <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">3.2</span><span class="token punctuation">)</span> <span class="token comment">// RangeError: Invalid array length</span>
  531. <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// RangeError: Invalid array length</span>
  532. <span class="token comment">// 单个非数值(比如字符串、布尔值、对象等)作为参数,</span>
  533. <span class="token comment">// 则该参数是返回的新数组的成员</span>
  534. <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span> <span class="token comment">// ['abc']</span>
  535. <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// [Array[1]]</span>
  536. <span class="token comment">// 多参数时,所有参数都是返回的新数组的成员</span>
  537. <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// [1, 2]</span>
  538. <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">)</span> <span class="token comment">// ['a', 'b', 'c']</span>
  539. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>可以看到,<code>Array</code>作为构造函数,行为很不一致。因此,不建议使用它生成新数组,直接使用数组字面量是更好的做法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// bad</span>
  540. <span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  541. <span class="token comment">// good</span>
  542. <span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  543. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>注意,如果参数是一个正整数,返回数组的成员都是空位。虽然读取的时候返回<code>undefined</code>,但实际上该位置没有任何值。虽然可以取到<code>length</code>属性,但是取不到键名。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  544. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token keyword">undefined</span><span class="token punctuation">,</span> <span class="token keyword">undefined</span><span class="token punctuation">,</span> <span class="token keyword">undefined</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  545. a<span class="token punctuation">.</span>length <span class="token comment">// 3</span>
  546. b<span class="token punctuation">.</span>length <span class="token comment">// 3</span>
  547. a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token comment">// undefined</span>
  548. b<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token comment">// undefined</span>
  549. <span class="token number">0</span> <span class="token keyword">in</span> a <span class="token comment">// false // in运算符用于数组时,前面的参数为数组索引</span>
  550. <span class="token number">0</span> <span class="token keyword">in</span> b <span class="token comment">// true</span>
  551. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,<code>a</code>是一个长度为3的空数组,<code>b</code>是一个三个成员都是<code>undefined</code>的数组。读取键值的时候,<code>a</code>和<code>b</code>都返回<code>undefined</code>,但是<code>a</code>的键位都是空的,<code>b</code>的键位是有值的。</p> <h2 id="_2、静态方法"><a href="#_2、静态方法" class="header-anchor">#</a> 2、静态方法</h2> <h3 id="array-isarray"><a href="#array-isarray" class="header-anchor">#</a> Array.isArray()</h3> <p><code>Array.isArray</code>方法返回一个布尔值,表示参数是否为数组。它可以弥补<code>typeof</code>运算符的不足。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span>
  552. <span class="token keyword">typeof</span> arr <span class="token comment">// object // 使用typeof返回的是object</span>
  553. Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token comment">// true</span>
  554. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,<code>typeof</code>运算符只能显示数组的类型是<code>Object</code>,而<code>Array.isArray</code>方法可以识别数组。</p> <h2 id="_3、实例方法"><a href="#_3、实例方法" class="header-anchor">#</a> 3、实例方法</h2> <h3 id="valueof-原数组-tostring-转字符串"><a href="#valueof-原数组-tostring-转字符串" class="header-anchor">#</a> valueOf() 原数组,toString() 转字符串</h3> <p><code>valueOf</code>方法是一个所有对象都拥有的方法,表示对该对象求值。不同对象的<code>valueOf</code>方法不尽一致,数组的<code>valueOf</code>方法<strong>返回数组本身</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span>
  555. arr<span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// [1,2,3]</span>
  556. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>toString</code>方法也是对象的通用方法,数组的<code>toString</code>方法返回数组的字符串形式。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  557. arr<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1,2,3&quot;</span>
  558. <span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  559. arr<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1,2,3,4,5,6&quot;</span>
  560. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="push-末尾添加-pop-删除末尾"><a href="#push-末尾添加-pop-删除末尾" class="header-anchor">#</a> push() 末尾添加,pop() 删除末尾</h3> <p><code>push</code>方法用于在数组的<strong>末端添加一个或多个元素</strong>,并返回添加新元素后的数组长度。注意,该方法会改变原数组。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">]</span>
  561. arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  562. arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">)</span> <span class="token comment">// 2</span>
  563. arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 4</span>
  564. arr <span class="token comment">// [1,'a',true,{}]</span>
  565. <span class="token comment">// 注:push()方法 返回添加后的数组长度,会改变原数组</span>
  566. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码使用<code>push</code>方法,往数组中添加了四个成员。</p> <p><code>pop</code>方法用于<strong>删除数组的最后一个元素</strong>,并返回该元素。注意,该方法会改变原数组。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">]</span>
  567. arr<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 'c' 删除最后一个元素,并返回该元素</span>
  568. arr <span class="token comment">// ['a','b']</span>
  569. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>对空数组使用<code>pop</code>方法,不会报错,而是返回<code>undefined</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// undefined</span>
  570. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>push</code>和<code>pop</code>结合使用,就构成了“后进先出”的<strong>栈结构</strong>(stack)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  571. arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  572. arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  573. arr<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  574. arr <span class="token comment">// [1, 2]</span>
  575. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,<code>3</code>是最后进入数组的,但是最早离开数组。</p> <h3 id="unshift-首位添加-shift-删除首位"><a href="#unshift-首位添加-shift-删除首位" class="header-anchor">#</a> unshift() 首位添加, shift() 删除首位</h3> <p><code>unshift()</code>方法用于在数组的<strong>第一个位置添加元素</strong>,并返回添加新元素后的数组长度。注意,该方法会改变原数组。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  576. a<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span><span class="token string">'x'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 4</span>
  577. a <span class="token comment">// ['x', 'a', 'b', 'c']</span>
  578. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>unshift()</code>方法可以接受多个参数,这些参数都会添加到目标数组头部。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span> <span class="token punctuation">]</span><span class="token punctuation">;</span>
  579. arr<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">)</span> <span class="token comment">// 4</span>
  580. arr <span class="token comment">// [ 'a', 'b', 'c', 'd' ]</span>
  581. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>shift()</code>方法用于<strong>删除数组的第一个元素</strong>,并返回该元素。注意,该方法会改变原数组。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">]</span>
  582. a<span class="token punctuation">.</span><span class="token function">shift</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 'a' 删除第一个元素,并返回该元素</span>
  583. a <span class="token comment">// ['b','c']</span>
  584. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,使用<code>shift()</code>方法以后,原数组就变了。</p> <p><code>shift()</code>方法可以遍历并清空一个数组。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> list <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  585. <span class="token keyword">var</span> item<span class="token punctuation">;</span>
  586. <span class="token keyword">while</span> <span class="token punctuation">(</span>item <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">shift</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  587. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
  588. <span class="token punctuation">}</span>
  589. list <span class="token comment">// []</span>
  590. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码通过<code>list.shift()</code>方法每次取出一个元素,从而遍历数组。它的前提是数组元素不能是<code>0</code>或任何布尔值等于<code>false</code>的元素,因此<strong>这样的遍历不是很可靠</strong>。</p> <p><code>push()</code>和<code>shift()</code>结合使用,就构成了“先进先出”的<strong>队列(堆)结构</strong>(queue)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> queue <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  591. queue<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'1'</span><span class="token punctuation">)</span>
  592. queue<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'2'</span><span class="token punctuation">)</span>
  593. queue<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'3'</span><span class="token punctuation">)</span>
  594. queue<span class="token punctuation">.</span><span class="token function">shift</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  595. queue <span class="token comment">// ['2','3']</span>
  596. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><blockquote><p>push() 最后位置添加元素,unshift() 第一位置添加元素</p> <p>pop() 删除最后一个元素,shift() 删除第一个元素</p></blockquote> <h3 id="join-连接每个成员-转字符串"><a href="#join-连接每个成员-转字符串" class="header-anchor">#</a> join() 连接每个成员-转字符串</h3> <p><code>join()</code>方法以指定参数作为分隔符,<strong>将所有数组成员连接为一个字符串返回</strong>。如果不提供参数,默认用逗号分隔。</p> <p><strong>数组转字符串</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span>
  597. a<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span> <span class="token comment">// '1234'</span>
  598. a<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">' '</span><span class="token punctuation">)</span> <span class="token comment">// '1 2 3 4'</span>
  599. a<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'|'</span><span class="token punctuation">)</span> <span class="token comment">// '1|2|3|4‘</span>
  600. a<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// '1,2,3,4'</span>
  601. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>如果数组成员是<code>undefined</code>或<code>null</code>或空位,会被转成空字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token keyword">undefined</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'#'</span><span class="token punctuation">)</span>
  602. <span class="token comment">// '#'</span>
  603. <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'-'</span><span class="token punctuation">)</span>
  604. <span class="token comment">// 'a--b'</span>
  605. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>通过<code>call</code>方法,这个方法也可以用于字符串或类似数组的对象。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token string">'hello'</span><span class="token punctuation">,</span> <span class="token string">'-'</span><span class="token punctuation">)</span>
  606. <span class="token comment">// &quot;h-e-l-l-o&quot;</span>
  607. <span class="token comment">// 等价 'hello'.split('').join('-')</span>
  608. <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token number">0</span><span class="token operator">:</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token operator">:</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token literal-property property">length</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// 数组的变形对象</span>
  609. <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token string">'-'</span><span class="token punctuation">)</span>
  610. <span class="token comment">// 'a-b'</span>
  611. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="concat-合并数组"><a href="#concat-合并数组" class="header-anchor">#</a> concat() 合并数组</h3> <p><code>concat</code>方法用于<strong>多个数组的合并</strong>。它将新数组的成员,添加到原数组成员的后部,然后<strong>返回一个新数组,原数组不变</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token string">'hello'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'world'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  612. <span class="token comment">// ['hello','world']</span>
  613. <span class="token punctuation">[</span><span class="token string">'hello'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'world'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'!'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  614. <span class="token comment">// [&quot;hello&quot;, &quot;world&quot;, &quot;!&quot;]</span>
  615. <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  616. <span class="token comment">// [{ a: 1 }, { b: 2 }]</span>
  617. <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  618. <span class="token comment">// [2, {a: 1}]</span>
  619. <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>
  620. <span class="token comment">// [1,2]</span>
  621. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>除了数组作为参数,<code>concat</code>也接受其他类型的值作为参数,添加到目标数组尾部。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span>
  622. <span class="token comment">// [1, 2, 3, 4, 5, 6]</span>
  623. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>如果数组成员包括对象,<code>concat</code>方法返回当前数组的一个浅拷贝。所谓“<strong>浅拷贝</strong>”,指的是新数组拷贝的是对象的引用。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
  624. <span class="token keyword">var</span> oldArray <span class="token operator">=</span> <span class="token punctuation">[</span>obj<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 浅拷贝</span>
  625. <span class="token keyword">var</span> newArray <span class="token operator">=</span> oldArray<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  626. obj<span class="token punctuation">.</span>a <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment">// 改变原对象以后,新数组跟着改变</span>
  627. newArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>a <span class="token comment">// 2</span>
  628. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中,原数组包含一个对象,<code>concat</code>方法生成的新数组包含这个对象的引用。所以,<strong>改变原对象以后,新数组跟着改变</strong>。</p> <h3 id="reverse-反转"><a href="#reverse-反转" class="header-anchor">#</a> reverse() 反转</h3> <p><code>reverse</code>方法用于<strong>颠倒排列数组元素</strong>,返回改变后的数组。注意,该方法将<strong>改变原数组</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span>
  629. a<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// [4,3,2,1]</span>
  630. a <span class="token comment">// [4,3,2,1]</span>
  631. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="slice-部分-提取部分成员"><a href="#slice-部分-提取部分成员" class="header-anchor">#</a> slice() 部分-提取部分成员</h3> <p><code>slice</code>方法用于<strong>提取目标数组的一部分</strong>,返回一个新数组,<strong>原数组不变</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span> end<span class="token punctuation">)</span><span class="token punctuation">;</span>
  632. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>它的第一个参数为起始位置(从0开始),第二个参数为终止位置(但该位置的元素本身不包括在内)。如果省略第二个参数,则一直返回到原数组的最后一个成员。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">,</span><span class="token string">'c'</span><span class="token punctuation">]</span>
  633. a<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">// ['a','b','c'] 从0到最后一个成员</span>
  634. a<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// ['b','c'] 从1到最后一个成员</span>
  635. a<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// ['b'] 从1到2成员,不包含2成员</span>
  636. a<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment">// ['c'] 从2到6成员,超出的不计算在内</span>
  637. a<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//['a','b','c'] 无参数,默认全部,多用于拷贝数组到新的数组</span>
  638. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,最后一个例子<code>slice</code>没有参数,实际上等于返回一个原数组的拷贝。</p> <p>如果<code>slice</code>方法的参数是<strong>负数</strong>,则表示<strong>倒数计算的位置</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  639. a<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;b&quot;, &quot;c&quot;]</span>
  640. a<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;b&quot;]</span>
  641. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,<code>-2</code>表示倒数计算的第二个位置,<code>-1</code>表示倒数计算的第一个位置。</p> <p>如果<strong>第一个参数大于等于数组长度</strong>,或者<strong>第二个参数小于第一个参数</strong>,则<strong>返回空数组</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  642. a<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// []</span>
  643. a<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// []</span>
  644. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="应用-类数组转数组"><a href="#应用-类数组转数组" class="header-anchor">#</a> 应用-类数组转数组</h4> <p>slice`方法的<strong>一个重要应用</strong>,是<strong>将类似数组的对象转为真正的数组</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token number">0</span><span class="token operator">:</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token operator">:</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token literal-property property">length</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
  645. <span class="token comment">// ['a', 'b']</span>
  646. <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>document<span class="token punctuation">.</span><span class="token function">querySelectorAll</span><span class="token punctuation">(</span><span class="token string">&quot;div&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  647. <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
  648. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码的参数都不是数组,但是通过<code>call</code>方法,在它们上面调用<code>slice</code>方法,就可以把它们转为真正的数组。</p> <h3 id="splice-从第几个开始-删除几个-并添加成员-返回被删除成员。"><a href="#splice-从第几个开始-删除几个-并添加成员-返回被删除成员。" class="header-anchor">#</a> splice() 从第几个开始,删除几个,并添加成员,返回被删除成员。</h3> <p><code>splice</code>方法用于<strong>删除原数组的一部分成员</strong>,<strong>并可以在删除的位置添加新的数组成员</strong>,<strong>返回值是被删除的元素</strong>。注意,该方法会<strong>改变原数组</strong>。</p> <blockquote><p>也可以不删除成员同时在指定位置添加新成员</p></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code>arr<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span> count<span class="token punctuation">,</span> addElement1<span class="token punctuation">,</span> addElement2<span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  649. <span class="token comment">// 第几个开始,删除几个,加入的新成员...(非必需)</span>
  650. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>splice</code>的第一个参数是删除的起始位置(从0开始),第二个参数是被删除的元素个数。如果后面还有更多的参数,则表示这些就是要被插入数组的新元素。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
  651. a<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// [3,4] 返回被删除成员</span>
  652. a<span class="token punctuation">.</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
  653. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码从原数组2号位置,删除了2个数组成员。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">,</span> <span class="token string">'e'</span><span class="token punctuation">,</span> <span class="token string">'f'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  654. a<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;e&quot;, &quot;f&quot;]</span>
  655. a <span class="token comment">// [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, 1, 2]</span>
  656. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码从原数组4号位置删除2个成员,并插入了[1 , 2]新成员。</p> <p>起始位置如果是负数,就表示从倒数位置开始删除。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">,</span> <span class="token string">'e'</span><span class="token punctuation">,</span> <span class="token string">'f'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  657. a<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;c&quot;, &quot;d&quot;] 负数表示倒数</span>
  658. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码表示,从倒数第四个位置<code>c</code>开始删除两个成员。</p> <p>如果只是单纯地插入元素,<code>splice</code>方法的第二个参数可以设为<code>0</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  659. a<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">)</span> <span class="token comment">// [] // 不删除成员,并加入新成员到指定位置</span>
  660. a <span class="token comment">// [1, 'a', 1, 1]</span>
  661. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h4 id="应用-拆分数组"><a href="#应用-拆分数组" class="header-anchor">#</a> 应用-拆分数组</h4> <p>如果只提供第一个参数,等同于将原数组在指定位置<strong>拆分成两个数组</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  662. <span class="token keyword">var</span> a2 <span class="token operator">=</span> a<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// [3, 4]</span>
  663. a <span class="token comment">// [1, 2]</span>
  664. a2 <span class="token comment">// [3, 4]</span>
  665. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="sort-排序"><a href="#sort-排序" class="header-anchor">#</a> sort() 排序</h3> <p><code>sort</code>方法对数组成员进行排序,默认是按照<strong>字典顺序排序</strong>。排序后,将<strong>改变原数组</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token string">'d'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  666. <span class="token comment">// ['a', 'b', 'c', 'd']</span>
  667. <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  668. <span class="token comment">// [1, 2, 3, 4]</span>
  669. <span class="token punctuation">[</span><span class="token string">'abc'</span><span class="token punctuation">,</span><span class="token string">'ba'</span><span class="token punctuation">,</span><span class="token string">'ea'</span><span class="token punctuation">,</span><span class="token string">'ae'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  670. <span class="token comment">// [&quot;abc&quot;, &quot;ae&quot;, &quot;ba&quot;, &quot;ea&quot;]</span>
  671. <span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">101</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  672. <span class="token comment">// [101, 11]</span>
  673. <span class="token punctuation">[</span><span class="token number">10111</span><span class="token punctuation">,</span> <span class="token number">1101</span><span class="token punctuation">,</span> <span class="token number">111</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  674. <span class="token comment">// [10111, 1101, 111]</span>
  675. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>上面代码的最后三个例子,需要特殊注意。<code>sort()</code>方法不是按照大小排序,而是<strong>按照字典顺序</strong>。也就是说,数值会被先转成字符串,再按照字典顺序进行比较,所以<code>101</code>排在<code>11</code>的前面。</p> <blockquote><p>先转成字符串,再从0位置开始对比对应位置上的值的顺序</p></blockquote> <p>如果想让<code>sort</code>方法按照自定义方式排序,可以传入一个函数作为参数。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token number">10111</span><span class="token punctuation">,</span> <span class="token number">1101</span><span class="token punctuation">,</span> <span class="token number">111</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 返回值大于0,则a排于b之后,否则反之。</span>
  676. <span class="token keyword">return</span> a <span class="token operator">-</span> b<span class="token punctuation">;</span> <span class="token comment">// 升序</span>
  677. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  678. <span class="token comment">// [111, 1101, 10111]</span>
  679. <span class="token punctuation">[</span><span class="token number">10111</span><span class="token punctuation">,</span> <span class="token number">1101</span><span class="token punctuation">,</span> <span class="token number">111</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  680. <span class="token keyword">return</span> b <span class="token operator">-</span> a<span class="token punctuation">;</span> <span class="token comment">// 降序</span>
  681. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  682. <span class="token comment">// [10111,1101, 111]</span>
  683. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,<code>sort</code>的参数函数本身接受两个参数,表示进行比较的两个数组成员。如果该函数的返回值大于<code>0</code>,表示第一个成员排在第二个成员后面;其他情况下,都是第一个元素排在第二个元素前面。</p> <h4 id="应用-自定义排序"><a href="#应用-自定义排序" class="header-anchor">#</a> 应用-自定义排序</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span>
  684. <span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span> <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">30</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  685. <span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;李四&quot;</span><span class="token punctuation">,</span> <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">24</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  686. <span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;王五&quot;</span><span class="token punctuation">,</span> <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">28</span> <span class="token punctuation">}</span>
  687. <span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">o1<span class="token punctuation">,</span> o2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  688. <span class="token keyword">return</span> o1<span class="token punctuation">.</span>age <span class="token operator">-</span> o2<span class="token punctuation">.</span>age<span class="token punctuation">;</span>
  689. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  690. <span class="token comment">// [</span>
  691. <span class="token comment">// { name: &quot;李四&quot;, age: 24 },</span>
  692. <span class="token comment">// { name: &quot;王五&quot;, age: 28 },</span>
  693. <span class="token comment">// { name: &quot;张三&quot;, age: 30 }</span>
  694. <span class="token comment">// ]</span>
  695. <span class="token comment">// 自定义排序</span>
  696. <span class="token punctuation">[</span>
  697. <span class="token punctuation">{</span><span class="token literal-property property">day</span><span class="token operator">:</span> <span class="token string">'星期日'</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">day</span><span class="token operator">:</span><span class="token string">'星期一'</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">day</span><span class="token operator">:</span><span class="token string">'星期五'</span><span class="token punctuation">}</span>
  698. <span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">o1<span class="token punctuation">,</span>o2</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  699. <span class="token keyword">return</span> <span class="token function">computedDay</span><span class="token punctuation">(</span>o1<span class="token punctuation">.</span>day<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token function">computedDay</span><span class="token punctuation">(</span>o2<span class="token punctuation">.</span>day<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  700. <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">//[{ day: '星期一' }, { day: '星期五' }, { day: '星期日' }]</span>
  701. <span class="token keyword">function</span> <span class="token function">computedDay</span><span class="token punctuation">(</span><span class="token parameter">v</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  702. <span class="token keyword">return</span> v <span class="token operator">===</span> <span class="token string">'一'</span> <span class="token operator">?</span> <span class="token number">1</span> <span class="token operator">:</span> v <span class="token operator">===</span> <span class="token string">'二'</span> <span class="token operator">?</span> <span class="token number">2</span> <span class="token operator">:</span> v <span class="token operator">===</span> <span class="token string">'三'</span> <span class="token operator">?</span> <span class="token number">3</span> <span class="token operator">:</span> v <span class="token operator">===</span> <span class="token string">'四'</span> <span class="token operator">?</span> <span class="token number">4</span> <span class="token operator">:</span> v <span class="token operator">===</span> <span class="token string">'五'</span> <span class="token operator">?</span> <span class="token number">5</span> <span class="token operator">:</span> v <span class="token operator">===</span> <span class="token string">'六'</span> <span class="token operator">?</span> <span class="token number">6</span> <span class="token operator">:</span> <span class="token number">7</span>
  703. <span class="token punctuation">}</span>
  704. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><p>注意,<strong>自定义的排序函数应该返回数值</strong>,否则不同的浏览器可能有不同的实现,不能保证结果都一致。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// bad</span>
  705. <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> a <span class="token operator">&gt;</span> b<span class="token punctuation">)</span> <span class="token comment">// 返回布尔值,不推荐</span>
  706. <span class="token comment">// good</span>
  707. <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> a <span class="token operator">-</span> b<span class="token punctuation">)</span> <span class="token comment">// 返回数值,推荐</span>
  708. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,前一种排序算法返回的是布尔值,这是不推荐使用的。后一种是数值,才是更好的写法。</p> <h3 id="map-映射-每个成员-并返回"><a href="#map-映射-每个成员-并返回" class="header-anchor">#</a> map() 映射 每个成员 并返回</h3> <p><code>map</code>方法将数组的所有成员依次传入参数函数,然后把每一次的执行结果组成一个新数组返回。<strong>不改变原数组</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  709. numbers<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  710. <span class="token keyword">return</span> n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
  711. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  712. <span class="token comment">// [2, 3, 4]</span>
  713. numbers
  714. <span class="token comment">// [1, 2, 3]</span>
  715. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,<code>numbers</code>数组的所有成员依次执行参数函数,运行结果组成一个新数组返回,原数组没有变化。</p> <p><code>map</code>方法接受一个函数作为参数。该函数调用时,<code>map</code>方法向它传入三个参数:<strong>当前成员</strong>、<strong>当前索引</strong>、<strong>数组本身</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">elem<span class="token punctuation">,</span> index<span class="token punctuation">,</span> arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 当前成员、当前索引、数组本身</span>
  716. <span class="token keyword">return</span> elem <span class="token operator">*</span> index<span class="token punctuation">;</span>
  717. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  718. <span class="token comment">// [0, 2, 6]</span>
  719. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,<code>map</code>方法的回调函数有三个参数,<code>elem</code>为当前成员的值,<code>index</code>为当前成员的索引,<code>arr</code>为原数组(<code>[1, 2, 3]</code>)。</p> <p><code>map</code>方法还可以接受第二个参数,用来绑定回调函数内部的<code>this</code>变量(详见《this 变量》一章)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  720. <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  721. <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">[</span>e<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// this指向arr</span>
  722. <span class="token punctuation">}</span><span class="token punctuation">,</span> arr<span class="token punctuation">)</span> <span class="token comment">// 使内部this指向arr</span>
  723. <span class="token comment">// ['b', 'c']</span>
  724. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码通过<code>map</code>方法的第二个参数,将回调函数内部的<code>this</code>对象,指向<code>arr</code>数组。</p> <p>如果数组有空位,<code>map</code>方法的回调函数在这个位置不会执行,会跳过数组的空位。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> <span class="token function-variable function">f</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">'a'</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
  725. <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">undefined</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;, &quot;a&quot;, &quot;a&quot;]</span>
  726. <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;, &quot;a&quot;, &quot;a&quot;]</span>
  727. <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;, , &quot;a&quot;]</span>
  728. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,<code>map</code>方法不会跳过<code>undefined</code>和<code>null</code>,但是会跳过空位。</p> <h3 id="foreach-循环每个成员"><a href="#foreach-循环每个成员" class="header-anchor">#</a> forEach() 循环每个成员</h3> <p><code>forEach</code>方法与<code>map</code>方法很相似,也是对数组的所有成员依次执行参数函数。但是,<code>forEach</code>方法<strong>不返回值</strong>,<strong>只用来操作数据</strong>。这就是说,如果数组遍历的目的是为了得到返回值,那么使用<code>map</code>方法,否则使用<code>forEach</code>方法。</p> <p><code>forEach</code>的用法与<code>map</code>方法一致,参数是一个函数,该函数同样接受三个参数:<strong>当前成员、当前索引、整个数组</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">log</span><span class="token punctuation">(</span><span class="token parameter">element<span class="token punctuation">,</span> index<span class="token punctuation">,</span> array</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 当前成员、当前索引、数组本身</span>
  729. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'['</span> <span class="token operator">+</span> index <span class="token operator">+</span> <span class="token string">'] = '</span> <span class="token operator">+</span> element<span class="token punctuation">)</span><span class="token punctuation">;</span>
  730. <span class="token punctuation">}</span>
  731. <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>log<span class="token punctuation">)</span><span class="token punctuation">;</span>
  732. <span class="token comment">// [0] = 2</span>
  733. <span class="token comment">// [1] = 5</span>
  734. <span class="token comment">// [2] = 9</span>
  735. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码中,<code>forEach</code>遍历数组不是为了得到返回值,而是为了在屏幕输出内容,所以不必使用<code>map</code>方法。</p> <p><code>forEach</code>方法也可以接受第二个参数,绑定参数函数的<code>this</code>变量。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> out <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  736. <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">elem</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  737. <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>elem <span class="token operator">*</span> elem<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// this指向为out</span>
  738. <span class="token punctuation">}</span><span class="token punctuation">,</span> out<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 使内部this指向out</span>
  739. out <span class="token comment">// [1, 4, 9]</span>
  740. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中,空数组<code>out</code>是<code>forEach</code>方法的第二个参数,结果,回调函数内部的<code>this</code>关键字就指向<code>out</code>。</p> <p>注意,<code>forEach</code>方法<strong>无法中断执行</strong>,总是会将所有成员遍历完。如果希望符合某种条件时,就中断遍历,要使用<code>for</code>循环。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  741. <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  742. <span class="token keyword">if</span> <span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
  743. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  744. <span class="token punctuation">}</span>
  745. <span class="token comment">// 1</span>
  746. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,执行到数组的第二个成员时,就会中断执行。<code>forEach</code>方法做不到这一点。</p> <p><code>forEach</code>方法也会跳过数组的空位。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> <span class="token function-variable function">log</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  747. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  748. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  749. <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">undefined</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>log<span class="token punctuation">)</span>
  750. <span class="token comment">// 2</span>
  751. <span class="token comment">// NaN</span>
  752. <span class="token comment">// 3</span>
  753. <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>log<span class="token punctuation">)</span>
  754. <span class="token comment">// 2</span>
  755. <span class="token comment">// 1 // null+1 =&gt; 1</span>
  756. <span class="token comment">// 3</span>
  757. <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>log<span class="token punctuation">)</span>
  758. <span class="token comment">// 2</span>
  759. <span class="token comment">// 3</span>
  760. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>上面代码中,<code>forEach</code>方法不会跳过<code>undefined</code>和<code>null</code>,但会跳过空位。</p> <h3 id="filter-过滤"><a href="#filter-过滤" class="header-anchor">#</a> filter() 过滤</h3> <p><code>filter</code>方法用于过滤数组成员,满足条件的成员组成一个新数组返回。</p> <p>它的参数是一个函数,所有数组成员依次执行该函数,返回结果为<code>true</code>的成员组成一个新数组返回。</p> <p>该方法<strong>不会改变原数组</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">elem</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  761. <span class="token keyword">return</span> elem <span class="token operator">&gt;</span> <span class="token number">3</span>
  762. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  763. <span class="token comment">// [4,5]</span>
  764. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码将大于<code>3</code>的数组成员,作为一个<strong>新数组返回</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  765. arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>Boolean<span class="token punctuation">)</span>
  766. <span class="token comment">// [1, &quot;a&quot;]</span>
  767. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,<code>filter</code>方法返回数组<code>arr</code>里面所有布尔值为<code>true</code>的成员。</p> <p><code>filter</code>方法的参数函数可以接受三个参数**:当前成员,当前位置、整个数组。**</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">elem<span class="token punctuation">,</span> index<span class="token punctuation">,</span> arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 当前成员,当前位置、整个数组</span>
  768. <span class="token keyword">return</span> index <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">===</span> <span class="token number">0</span><span class="token punctuation">;</span>
  769. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  770. <span class="token comment">// [1, 3, 5]</span>
  771. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码返回偶数位置的成员组成的新数组。</p> <p><code>filter</code>方法还可以接受第二个参数,用来绑定参数函数内部的<code>this</code>变量。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token constant">MAX</span><span class="token operator">:</span> <span class="token number">3</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
  772. <span class="token keyword">var</span> <span class="token function-variable function">myFilter</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  773. <span class="token keyword">if</span> <span class="token punctuation">(</span>item <span class="token operator">&gt;</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token constant">MAX</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment">// this指向obj</span>
  774. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  775. <span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  776. arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>myFilter<span class="token punctuation">,</span> obj<span class="token punctuation">)</span> <span class="token comment">// [8, 4, 9] // 使myFilter内部this指向obj</span>
  777. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中,过滤器<code>myFilter</code>内部有<code>this</code>变量,它可以被<code>filter</code>方法的第二个参数<code>obj</code>绑定,返回大于<code>3</code>的成员。</p> <h3 id="some-某个-every-每个"><a href="#some-某个-every-每个" class="header-anchor">#</a> some()某个,every()每个</h3> <p>这两个方法类似“<strong>断言</strong>”(assert),返回一个布尔值,表示判断数组成员是否符合某种条件。</p> <p>它们接受一个函数作为参数,所有数组成员依次执行该函数。该函数接受三个参数:<strong>当前成员、当前位置、整个数组</strong>,然后<strong>返回一个布尔值</strong>。</p> <p><code>some</code>方法是只要一个成员的返回值是<code>true</code>,则整个<code>some</code>方法的返回值就是<code>true</code>,否则返回<code>false</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span>
  778. arr<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">elem<span class="token punctuation">,</span>index<span class="token punctuation">,</span>arr</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">// 返回值中有一个为true则此方法返回true</span>
  779. <span class="token keyword">return</span> elem <span class="token operator">&gt;=</span> <span class="token number">3</span>
  780. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  781. <span class="token comment">// true</span>
  782. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,如果数组<code>arr</code>有一个成员大于等于3,<code>some</code>方法就返回<code>true</code>。</p> <p><code>every</code>方法是所有成员的返回值都是<code>true</code>,整个<code>every</code>方法才返回<code>true</code>,否则返回<code>false</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  783. arr<span class="token punctuation">.</span><span class="token function">every</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">elem<span class="token punctuation">,</span> index<span class="token punctuation">,</span> arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 每个返回值都为true,此方法才返回true,否则返回false</span>
  784. <span class="token keyword">return</span> elem <span class="token operator">&gt;=</span> <span class="token number">3</span><span class="token punctuation">;</span>
  785. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  786. <span class="token comment">// false</span>
  787. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,数组<code>arr</code>并非所有成员大于等于<code>3</code>,所以返回<code>false</code>。</p> <p>注意,对于空数组,<code>some</code>方法返回<code>false</code>,<code>every</code>方法返回<code>true</code>,回调函数都不会执行。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">isEven</span><span class="token punctuation">(</span><span class="token parameter">x</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> x <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">===</span> <span class="token number">0</span> <span class="token punctuation">}</span> <span class="token comment">// 不会执行此函数</span>
  788. <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span>isEven<span class="token punctuation">)</span> <span class="token comment">// false</span>
  789. <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">every</span><span class="token punctuation">(</span>isEven<span class="token punctuation">)</span> <span class="token comment">// true</span>
  790. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>some</code>和<code>every</code>方法还可以接受第二个参数,用来绑定参数函数内部的<code>this</code>变量。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span>
  791. <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token constant">MAX</span><span class="token operator">:</span><span class="token number">0</span><span class="token punctuation">}</span>
  792. <span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token parameter">elem</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  793. <span class="token keyword">return</span> elem <span class="token operator">&gt;</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token constant">MAX</span> <span class="token comment">// this指向obj</span>
  794. <span class="token punctuation">}</span>
  795. arr<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span>fn<span class="token punctuation">,</span>obj<span class="token punctuation">)</span> <span class="token comment">// true</span>
  796. arr<span class="token punctuation">.</span><span class="token function">every</span><span class="token punctuation">(</span>fn<span class="token punctuation">,</span>obj<span class="token punctuation">)</span> <span class="token comment">// true</span>
  797. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="reduce-累计器-reduceright-反向累计器"><a href="#reduce-累计器-reduceright-反向累计器" class="header-anchor">#</a> reduce() 累计器,reduceRight() 反向累计器</h3> <p><code>reduce</code>方法和<code>reduceRight</code>方法依次处理数组的每个成员,最终累计为一个值。它们的差别是,<code>reduce</code>是从左到右处理(从第一个成员到最后一个成员),<code>reduceRight</code>则是从右到左(从最后一个成员到第一个成员),其他完全一样。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>c<span class="token punctuation">,</span>d</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  798. <span class="token comment">// 记住a是默认是第一个成员,然后是上一次运算的返回值。</span>
  799. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span>
  800. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>
  801. <span class="token keyword">return</span> a<span class="token operator">+</span>b
  802. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  803. <span class="token comment">// 1 2</span>
  804. <span class="token comment">// 3 3</span>
  805. <span class="token comment">// 6 4</span>
  806. <span class="token comment">// 10 5</span>
  807. <span class="token comment">// 最终结果 15</span>
  808. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,<code>reduce</code>方法求出数组所有成员的和。第一次执行,<code>a</code>是数组的第一个成员<code>1</code>,<code>b</code>是数组的第二个成员<code>2</code>。第二次执行,<code>a</code>为上一轮的返回值<code>3</code>,<code>b</code>为第三个成员<code>3</code>。第三次执行,<code>a</code>为上一轮的返回值<code>6</code>,<code>b</code>为第四个成员<code>4</code>。第四次执行,<code>a</code>为上一轮返回值<code>10</code>,<code>b</code>为第五个成员<code>5</code>。至此所有成员遍历完成,整个方法的返回值就是最后一轮的返回值<code>15</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token function">callback</span><span class="token punctuation">(</span>accumulator<span class="token punctuation">,</span> currentValue<span class="token punctuation">[</span><span class="token punctuation">,</span> index<span class="token punctuation">[</span><span class="token punctuation">,</span> array<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">,</span> initialValue<span class="token punctuation">]</span><span class="token punctuation">)</span>
  809. <span class="token comment">// 说明:中括号代表可选参数</span>
  810. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>reduce</code>方法和<code>reduceRight</code>方法的第一个参数都是一个<code>callback</code>函数。该函数接受以下四个参数。</p> <ol><li>累积变量,默认为数组的第一个成员</li> <li>当前变量,默认为数组的第二个成员</li> <li>当前位置(默认从1开始), 如果提供了<code>initialValue</code>,则起始索引号为0,否则为1。 (可选)</li> <li>原数组(可选)</li></ol> <p><code>initialValue</code>可选</p> <p>作为第一次调用 <code>callback函数时</code>的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错。</p> <p>如果要对累积变量指定初值,可以把它放在<code>reduce</code>方法和<code>reduceRight</code>方法的第二个参数。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  811. <span class="token keyword">return</span> a <span class="token operator">+</span> b<span class="token punctuation">;</span>
  812. <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  813. <span class="token comment">// 25</span>
  814. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码指定参数<code>a</code>的初值为10,所以数组从10开始累加,最终结果为25。注意,这时<code>b</code>是从数组的第一个成员开始遍历。</p> <p>上面的第二个参数相当于设定了默认值,处理空数组时尤其有用。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token parameter">prev<span class="token punctuation">,</span> cur</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  815. <span class="token keyword">return</span> prev <span class="token operator">+</span> cur<span class="token punctuation">;</span>
  816. <span class="token punctuation">}</span>
  817. <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span>add<span class="token punctuation">)</span> <span class="token comment">// 报错</span>
  818. <span class="token comment">// TypeError: Reduce of empty array with no initial value</span>
  819. <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span>add<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
  820. <span class="token comment">// 1</span>
  821. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码中,由于空数组取不到初始值,<code>reduce</code>方法会报错。这时,加上第二个参数,就能保证总是会返回一个值。</p> <p>下面是一个<code>reduceRight</code>方法的例子。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">subtract</span><span class="token punctuation">(</span><span class="token parameter">prev<span class="token punctuation">,</span> cur</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  822. <span class="token keyword">return</span> prev <span class="token operator">-</span> cur<span class="token punctuation">;</span>
  823. <span class="token punctuation">}</span>
  824. <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span>subtract<span class="token punctuation">)</span> <span class="token comment">// 0</span>
  825. <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">reduceRight</span><span class="token punctuation">(</span>subtract<span class="token punctuation">)</span> <span class="token comment">// -4</span>
  826. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,<code>reduce</code>方法相当于<code>3</code>减去<code>2</code>再减去<code>1</code>,<code>reduceRight</code>方法相当于<code>1</code>减去<code>2</code>再减去<code>3</code>。</p> <h4 id="应用-找出字符最长的数组成员"><a href="#应用-找出字符最长的数组成员" class="header-anchor">#</a> 应用-找出字符最长的数组成员</h4> <p>由于这两个方法会遍历数组,所以实际上还可以用来做一些遍历相关的操作。比如,找出字符长度最长的数组成员。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>function findLongest(entries) {
  827. return entries.reduce(function (longest, entry) {
  828. return entry.length &gt; longest.length ? entry : longest;
  829. }, '');
  830. }
  831. findLongest(['aaa', 'bb', 'c']) // &quot;aaa&quot;
  832. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中,<code>reduce</code>的参数函数会将字符长度较长的那个数组成员,作为累积值。这导致遍历所有成员之后,累积值就是字符长度最长的那个成员。</p> <h4 id="应用-按属性对object分类"><a href="#应用-按属性对object分类" class="header-anchor">#</a> 应用-按属性对object分类</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> people <span class="token operator">=</span> <span class="token punctuation">[</span>
  833. <span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Alice'</span><span class="token punctuation">,</span> <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">21</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  834. <span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Max'</span><span class="token punctuation">,</span> <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">20</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  835. <span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Jane'</span><span class="token punctuation">,</span> <span class="token literal-property property">age</span><span class="token operator">:</span> <span class="token number">20</span> <span class="token punctuation">}</span>
  836. <span class="token punctuation">]</span><span class="token punctuation">;</span>
  837. <span class="token keyword">function</span> <span class="token function">groupBy</span><span class="token punctuation">(</span><span class="token parameter">objectArray<span class="token punctuation">,</span> property</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  838. <span class="token keyword">return</span> objectArray<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">acc<span class="token punctuation">,</span> obj</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  839. <span class="token keyword">var</span> key <span class="token operator">=</span> obj<span class="token punctuation">[</span>property<span class="token punctuation">]</span><span class="token punctuation">;</span>
  840. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>acc<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">// 当{}中没有对应分类属性时,先创建改属性</span>
  841. acc<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  842. <span class="token punctuation">}</span>
  843. acc<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
  844. <span class="token keyword">return</span> acc<span class="token punctuation">;</span>
  845. <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 初始值为{}</span>
  846. <span class="token punctuation">}</span>
  847. <span class="token keyword">var</span> groupedPeople <span class="token operator">=</span> <span class="token function">groupBy</span><span class="token punctuation">(</span>people<span class="token punctuation">,</span> <span class="token string">'age'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  848. <span class="token comment">// groupedPeople is:</span>
  849. <span class="token comment">// {</span>
  850. <span class="token comment">// 20: [</span>
  851. <span class="token comment">// { name: 'Max', age: 20 },</span>
  852. <span class="token comment">// { name: 'Jane', age: 20 }</span>
  853. <span class="token comment">// ],</span>
  854. <span class="token comment">// 21: [{ name: 'Alice', age: 21 }]</span>
  855. <span class="token comment">// }</span>
  856. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><p>更多例子:&lt; https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce &gt;</p> <h3 id="indexof-搜索成员第一个出现的位置-lastindexof-搜索成员最后出现的位置"><a href="#indexof-搜索成员第一个出现的位置-lastindexof-搜索成员最后出现的位置" class="header-anchor">#</a> indexOf() 搜索成员第一个出现的位置,lastIndexOf() 搜索成员最后出现的位置</h3> <p><code>indexOf</code>方法返回<strong>给定元素在数组中第一次出现的位置</strong>,如果<strong>没有则返回<code>-1</code></strong>。</p> <p><strong>indexOf语法</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>arr<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>searchElement<span class="token punctuation">[</span><span class="token punctuation">,</span> fromIndex <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  857. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span>
  858. a<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// 2 3在数组a中第一次出现的位置为2</span>
  859. a<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment">// -1 6在数组a中没有出现,返回-1</span>
  860. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>indexOf</code>方法还可以接受第二个参数,表示搜索的开始位置。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// -1 从1位置开始搜索a,没有搜索到</span>
  861. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码从1号位置开始搜索字符<code>a</code>,结果为<code>-1</code>,表示没有搜索到。</p> <p><strong>lastIndexOf()语法</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>arr.lastIndexOf(searchElement[, fromIndex = arr.length - 1])
  862. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>lastIndexOf</code>方法返回<strong>给定元素在数组中最后一次出现的位置</strong>,如果没有则返回<code>-1</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span>
  863. a<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">//3 2在数组a中最后一次出现的位置为3</span>
  864. a<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token comment">//-1</span>
  865. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>注意,这两个方法不能用来搜索<code>NaN</code>的位置,即它们无法确定数组成员是否包含<code>NaN</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token number">NaN</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">)</span> <span class="token comment">// -1</span>
  866. <span class="token punctuation">[</span><span class="token number">NaN</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">)</span> <span class="token comment">// -1</span>
  867. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>这是因为这两个方法内部,使用严格相等运算符(<code>===</code>)进行比较,而<code>NaN</code>是唯一一个不等于自身的值。</p> <h3 id="链式使用"><a href="#链式使用" class="header-anchor">#</a> 链式使用</h3> <p>上面这些数组方法之中,有不少返回的还是数组,所以可以链式使用。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> users <span class="token operator">=</span> <span class="token punctuation">[</span>
  868. <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'tom'</span><span class="token punctuation">,</span> <span class="token literal-property property">email</span><span class="token operator">:</span> <span class="token string">'tom@example.com'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  869. <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'peter'</span><span class="token punctuation">,</span> <span class="token literal-property property">email</span><span class="token operator">:</span> <span class="token string">'peter@example.com'</span><span class="token punctuation">}</span>
  870. <span class="token punctuation">]</span><span class="token punctuation">;</span>
  871. users
  872. <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  873. <span class="token keyword">return</span> user<span class="token punctuation">.</span>email<span class="token punctuation">;</span>
  874. <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 对每个成员进行遍历,使用返回值组成一个新的数组并返回</span>
  875. <span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">email</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 过滤返回值为false的成员,并返回一个过滤掉的新数组</span>
  876. <span class="token keyword">return</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^t</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>email<span class="token punctuation">)</span><span class="token punctuation">;</span>
  877. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  878. <span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">email</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 遍历数组</span>
  879. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>email<span class="token punctuation">)</span><span class="token punctuation">;</span>
  880. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  881. <span class="token comment">// &quot;tom@example.com&quot;</span>
  882. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>上面代码中,先产生一个所有 Email 地址组成的数组,然后再过滤出以<code>t</code>开头的 Email 地址,最后将它打印出来。</p> <blockquote><p>方法的返回值能满足下一个方法的调用,即可使用链式调用。</p></blockquote> <h2 id="四、包装对象"><a href="#四、包装对象" class="header-anchor">#</a> 四、包装对象</h2> <h2 id="_1、定义"><a href="#_1、定义" class="header-anchor">#</a> 1、定义</h2> <p>对象是JavaScript语言中最主要的数据类型,三种原始类型的值——数字、字符串、布尔值 在一定条件下,也会自动转为对象,也就是原始类型的<strong>包装对象</strong>(wrapper)</p> <p>所谓“包装对象”,<strong>指的是与数字、字符串、布尔值分别相对应的<code>Number</code>、<code>String</code>、<code>Boolean</code>三个原生对象。这三个原生对象可以把原始类型的值变成(包装成)对象</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> v1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Number</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  883. <span class="token keyword">var</span> v2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  884. <span class="token keyword">var</span> v3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  885. <span class="token keyword">typeof</span> v1 <span class="token comment">// &quot;object&quot;</span>
  886. <span class="token keyword">typeof</span> v2 <span class="token comment">// &quot;object&quot;</span>
  887. <span class="token keyword">typeof</span> v3 <span class="token comment">// &quot;object&quot;</span>
  888. v1 <span class="token operator">===</span> <span class="token number">123</span> <span class="token comment">// false</span>
  889. v2 <span class="token operator">===</span> <span class="token string">'abc'</span> <span class="token comment">// false</span>
  890. v3 <span class="token operator">===</span> <span class="token boolean">true</span> <span class="token comment">// false</span>
  891. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,基于原始类型的值,生成了三个对应的包装对象。可以看到,<code>v1</code>、<code>v2</code>、<code>v3</code>都是对象,且与对应的简单类型值不相等。</p> <p><strong>包装对象的设计目的,首先是使得“对象”这种类型可以覆盖 JavaScript 所有的值,整门语言有一个通用的数据模型,其次是使得原始类型的值也有办法调用自己的方法。</strong></p> <p><code>Number</code>、<code>String</code>和<code>Boolean</code>这三个原生对象,如果不作为构造函数调用(即调用时不加<code>new</code>),而是作为普通函数调用,常常用于将任意类型的值转为数值、字符串和布尔值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 字符串转为数值</span>
  892. <span class="token function">Number</span><span class="token punctuation">(</span><span class="token string">'123'</span><span class="token punctuation">)</span> <span class="token comment">// 123</span>
  893. <span class="token comment">// 数值转为字符串</span>
  894. <span class="token function">String</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span> <span class="token comment">// &quot;123&quot;</span>
  895. <span class="token comment">// 数值转为布尔值</span>
  896. <span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  897. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面这种<strong>数据类型的转换</strong>,详见《数据类型转换》一节。</p> <p>总结一下,这三个对象作为构造函数使用(带有<code>new</code>)时,可以将原始类型的值转为对象;作为普通函数使用时(不带有<code>new</code>),可以将任意类型的值,转为原始类型的值。</p> <h2 id="_2、实例方法"><a href="#_2、实例方法" class="header-anchor">#</a> 2、实例方法</h2> <p>三种包装对象各自提供了许多实例方法,详见后文。这里介绍两种它们共同具有、从<code>Object</code>对象<strong>继承的方法</strong>:<code>valueOf()</code>和<code>toString()</code>。</p> <h3 id="_2-1-valueof"><a href="#_2-1-valueof" class="header-anchor">#</a> 2.1 valueOf()</h3> <p><code>valueOf()</code>方法返回包装对象实例对应的原始类型的值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">new</span> <span class="token class-name">Number</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 123</span>
  898. <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;abc&quot;</span>
  899. <span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  900. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_2-2-tostring"><a href="#_2-2-tostring" class="header-anchor">#</a> 2.2 toString()</h3> <p><code>toString()</code>方法返回对应的字符串形式。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">new</span> <span class="token class-name">Number</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;123&quot;</span>
  901. <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;abc&quot;</span>
  902. <span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;true&quot;</span>
  903. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="_3、原始类型与实例对象的自动转换"><a href="#_3、原始类型与实例对象的自动转换" class="header-anchor">#</a> 3、原始类型与实例对象的自动转换</h2> <p>某些场合,原始类型的值会自动当作包装对象调用,即调用包装对象的属性和方法。这时,JavaScript 引擎会自动将原始类型的值转为包装对象实例,并在使用后立刻销毁实例。</p> <p>比如,字符串可以调用<code>length</code>属性,返回字符串的长度。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'abc'</span><span class="token punctuation">.</span>length <span class="token comment">// 3</span>
  904. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中,<code>abc</code>是一个字符串,本身不是对象,不能调用<code>length</code>属性。JavaScript 引擎自动将其转为包装对象,在这个对象上调用<code>length</code>属性。调用结束后,这个临时对象就会被销毁。这就叫原始类型与实例对象的自动转换。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'abc'</span><span class="token punctuation">;</span>
  905. str<span class="token punctuation">.</span>length <span class="token comment">// 3</span>
  906. <span class="token comment">//等同于</span>
  907. <span class="token keyword">var</span> strObj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span>
  908. <span class="token comment">// String{</span>
  909. <span class="token comment">// 0:'a',1:'b',2:'c',length:3,[[PrimitiveValue]]: &quot;abc&quot;</span>
  910. <span class="token comment">// }</span>
  911. strObj<span class="token punctuation">.</span>length <span class="token comment">// 3</span>
  912. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,字符串<code>abc</code>的包装对象提供了多个属性,<code>length</code>只是其中之一。</p> <p>自动转换生成的包装对象是只读的,无法修改。所以,字符串无法添加新属性。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'Hello World'</span><span class="token punctuation">;</span>
  913. s<span class="token punctuation">.</span>x <span class="token operator">=</span> <span class="token number">123</span><span class="token punctuation">;</span>
  914. s<span class="token punctuation">.</span>x <span class="token comment">// undefined</span>
  915. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码为字符串<code>s</code>添加了一个<code>x</code>属性,结果无效,总是返回<code>undefined</code>。</p> <p>另一方面,调用结束后,包装对象实例会自动销毁。这意味着,下一次调用字符串的属性时,实际是调用一个新生成的对象,而不是上一次调用时生成的那个对象,所以取不到赋值在上一个对象的属性。如果要为字符串添加属性,只有在它的原型对象<code>String.prototype</code>上定义(参见《面向对象编程》章节)。</p> <h2 id="_4、自定义方法"><a href="#_4、自定义方法" class="header-anchor">#</a> 4、自定义方法</h2> <p>除了原生的实例方法,包装对象还可以自定义方法和属性,供原始类型的值直接调用。</p> <p>比如,我们可以新增一个<code>double</code>方法,使得字符串和数字翻倍。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token class-name">String</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">double</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// String是一个方法,String.prototype本质是一个实例对象</span>
  916. <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  917. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  918. <span class="token string">'abc'</span><span class="token punctuation">.</span><span class="token function">double</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  919. <span class="token comment">// abcabc</span>
  920. <span class="token class-name">Number</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">double</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  921. <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  922. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  923. <span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">double</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 246 // 不加括号时点运算符会被解释成小数点</span>
  924. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>上面代码在<code>String</code>和<code>Number</code>这两个对象的原型上面,分别自定义了一个方法,从而可以在所有实例对象上调用。注意,最后一张的<code>123</code>外面必须要加上圆括号,否则后面的点运算符(<code>.</code>)会被解释成小数点。</p> <h2 id="五、boolean对象"><a href="#五、boolean对象" class="header-anchor">#</a> 五、Boolean对象</h2> <h2 id="_1、概述-3"><a href="#_1、概述-3" class="header-anchor">#</a> 1、概述</h2> <p><code>Boolean</code>对象是 JavaScript 的三个包装对象之一。作为构造函数,它主要用于生成布尔值的包装对象实例。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
  925. <span class="token keyword">typeof</span> b <span class="token comment">// object</span>
  926. b<span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  927. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码的变量<code>b</code>是一个<code>Boolean</code>对象的实例,它的类型是对象,值为布尔值<code>true</code>。</p> <p>注意,<code>false</code>对应的包装对象实例,布尔运算结果也是<code>true</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 返回的是一个对象,所有对象对应的布尔值都是true</span>
  928. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'true'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  929. <span class="token punctuation">}</span> <span class="token comment">// true</span>
  930. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 它的值为false</span>
  931. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'true'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  932. <span class="token punctuation">}</span> <span class="token comment">// 无输出</span>
  933. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码的第一个例子之所以得到<code>true</code>,是因为<code>false</code>对应的包装对象实例是一个对象,进行逻辑运算时,被自动转化成布尔值<code>true</code>(因为所有对象对应的布尔值都是<code>true</code>)。而实例的<code>valueOf</code>方法,则返回实例对应的原始值,本例为<code>false</code>。</p> <h2 id="_2、boolean-函数的类型转换作用"><a href="#_2、boolean-函数的类型转换作用" class="header-anchor">#</a> 2、Boolean 函数的类型转换作用</h2> <p><code>Boolean</code>对象除了可以作为构造函数,还可以单独使用,将任意值转为布尔值。这时<code>Boolean</code>就是一个<strong>单纯的工具方法</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token keyword">undefined</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  934. <span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  935. <span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  936. <span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  937. <span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  938. <span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  939. <span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token string">'false'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  940. <span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  941. <span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  942. <span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  943. <span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  944. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>上面代码中几种得到<code>true</code>的情况,<strong>都值得认真记住</strong>。</p> <blockquote><p>直接使用上面方法括号内的参数进行if逻辑运行相当于调用了Boolean方法并传入参数</p></blockquote> <p>顺便提一下,使用双重的否运算符(<code>!</code>)也可以将任意值转为对应的布尔值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">!</span><span class="token operator">!</span><span class="token keyword">undefined</span> <span class="token comment">// false</span>
  945. <span class="token operator">!</span><span class="token operator">!</span><span class="token keyword">null</span> <span class="token comment">// false</span>
  946. <span class="token operator">!</span><span class="token operator">!</span><span class="token number">0</span> <span class="token comment">// false</span>
  947. <span class="token operator">!</span><span class="token operator">!</span><span class="token string">''</span> <span class="token comment">// false</span>
  948. <span class="token operator">!</span><span class="token operator">!</span><span class="token number">NaN</span> <span class="token comment">// false</span>
  949. <span class="token operator">!</span><span class="token operator">!</span><span class="token number">1</span> <span class="token comment">// true</span>
  950. <span class="token operator">!</span><span class="token operator">!</span><span class="token string">'false'</span> <span class="token comment">// true</span>
  951. <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token comment">// true</span>
  952. <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// true</span>
  953. <span class="token operator">!</span><span class="token operator">!</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment">// true</span>
  954. <span class="token operator">!</span><span class="token operator">!</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo</span><span class="token regex-delimiter">/</span></span> <span class="token comment">// true</span>
  955. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>最后,对于一些特殊值,<code>Boolean</code>对象前面加不加<code>new</code>,会得到完全相反的结果,必须小心。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  956. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'true'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  957. <span class="token punctuation">}</span> <span class="token comment">// 无输出</span>
  958. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 加new 转为对象,对象为true</span>
  959. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'true'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  960. <span class="token punctuation">}</span> <span class="token comment">// true</span>
  961. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">Boolean</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  962. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'true'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  963. <span class="token punctuation">}</span> <span class="token comment">// 无输出</span>
  964. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Boolean</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 加new 转为对象,对象为true</span>
  965. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'true'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  966. <span class="token punctuation">}</span> <span class="token comment">// true</span>
  967. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="六、number对象"><a href="#六、number对象" class="header-anchor">#</a> 六、Number对象</h2> <h2 id="_1、概述-4"><a href="#_1、概述-4" class="header-anchor">#</a> 1、概述</h2> <p><code>Number</code>对象是数值对应的包装对象,可以作为构造函数使用,也可以作为工具函数使用。</p> <p>作为构造函数时,它用于生成值为数值的对象。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> n <span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Number</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 作为构造函数时,生成值为数值的对象</span>
  968. <span class="token keyword">typeof</span> n <span class="token comment">// object</span>
  969. n<span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  970. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,<code>Number</code>对象作为构造函数使用,返回一个值为<code>1</code>的对象。</p> <p>作为工具函数时,它可以将任何类型的值转为数值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">Number</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  971. <span class="token function">Number</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token comment">// 0</span>
  972. <span class="token function">Number</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token comment">// 0</span>
  973. <span class="token function">Number</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// 0</span>
  974. <span class="token function">Number</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span> <span class="token comment">// 0</span>
  975. <span class="token function">Number</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// NaN</span>
  976. <span class="token function">Number</span><span class="token punctuation">(</span><span class="token keyword">undefined</span><span class="token punctuation">)</span> <span class="token comment">// NaN</span>
  977. <span class="token function">Number</span><span class="token punctuation">(</span><span class="token string">'123'</span><span class="token punctuation">)</span><span class="token comment">// 123</span>
  978. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码将布尔值<code>true</code>转为数值<code>1</code>。<code>Number</code>作为工具函数的用法,详见《数据类型转换》一章。</p> <h2 id="_2、静态属性"><a href="#_2、静态属性" class="header-anchor">#</a> 2、静态属性</h2> <p><code>Number</code>对象拥有以下一些静态属性(即直接定义在<code>Number</code>对象上的属性,而不是定义在实例上的属性)。</p> <ul><li><code>Number.POSITIVE_INFINITY</code>:正的无限,指向<code>Infinity</code>。</li> <li><code>Number.NEGATIVE_INFINITY</code>:负的无限,指向<code>-Infinity</code>。</li> <li><code>Number.NaN</code>:表示非数值,指向<code>NaN</code>。</li> <li><code>Number.MAX_VALUE</code>:表示最大的正数<code>1.7976931348623157e+308</code>,相应的,最小的负数为<code>-Number.MAN_VALUE</code>。</li> <li><code>Number.MIN_VALUE</code>:表示最小的正数(即最接近0的正数,在64位浮点数体系中为<code>5e-324</code>),相应的,最接近0的负数为<code>-Number.MIN_VALUE</code>。</li> <li><code>Number.MAX_SAFE_INTEGER</code>:表示能够精确表示的最大整数,即<code>9007199254740991</code>。</li> <li><code>Number.MIN_SAFE_INTEGER</code>:表示能够精确表示的最小整数,即<code>-9007199254740991</code>。</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Number<span class="token punctuation">.</span><span class="token constant">POSITIVE_INFINITY</span> <span class="token comment">// Infinity</span>
  979. Number<span class="token punctuation">.</span><span class="token constant">NEGATIVE_INFINITY</span> <span class="token comment">// -Infinity</span>
  980. Number<span class="token punctuation">.</span><span class="token number">NaN</span> <span class="token comment">// NaN</span>
  981. Number<span class="token punctuation">.</span><span class="token constant">MAX_VALUE</span>
  982. <span class="token comment">// 1.7976931348623157e+308</span>
  983. Number<span class="token punctuation">.</span><span class="token constant">MAX_VALUE</span> <span class="token operator">&lt;</span> <span class="token number">Infinity</span>
  984. <span class="token comment">// true</span>
  985. Number<span class="token punctuation">.</span><span class="token constant">MIN_VALUE</span>
  986. <span class="token comment">// 5e-324</span>
  987. Number<span class="token punctuation">.</span><span class="token constant">MIN_VALUE</span> <span class="token operator">&gt;</span> <span class="token number">0</span>
  988. <span class="token comment">// true</span>
  989. Number<span class="token punctuation">.</span><span class="token constant">MAX_SAFE_INTEGER</span> <span class="token comment">// 9007199254740991</span>
  990. Number<span class="token punctuation">.</span><span class="token constant">MIN_SAFE_INTEGER</span> <span class="token comment">// -9007199254740991</span>
  991. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h2 id="_3、实例方法-2"><a href="#_3、实例方法-2" class="header-anchor">#</a> 3、实例方法</h2> <p><code>Number</code>对象有<strong>4个实例方法</strong>,都跟<strong>将数值转换成指定格式有关</strong>。</p> <h3 id="_3-1-number-prototype-tostring-可转成多种进制字符串"><a href="#_3-1-number-prototype-tostring-可转成多种进制字符串" class="header-anchor">#</a> 3.1 Number.prototype.toString() 可转成多种进制字符串</h3> <p><code>Number</code>对象部署了自己的<code>toString</code>方法,用来将一个数值转为字符串形式。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;10&quot;</span>
  992. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>toString</code>方法可以<strong>接受一个参数</strong>,表示输出的<strong>进制</strong>。如果省略这个参数,默认将数值先转为十进制,再输出字符串;否则,就根据参数指定的进制,将一个数字转化成某个进制的字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1010&quot;</span>
  993. <span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span> <span class="token comment">// &quot;12&quot;</span>
  994. <span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span> <span class="token comment">// &quot;a&quot;</span>
  995. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,<code>10</code>一定要放在括号里,这样表明后面的点表示调用对象属性。如果不加括号,这个点会被 JavaScript 引擎解释成小数点,从而报错。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token number">10.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// 不加括号会把点解释成小数点,从而报错</span>
  996. <span class="token comment">// SyntaxError: Unexpected token ILLEGAL</span>
  997. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>只要能够让 JavaScript 引擎不混淆小数点和对象的点运算符,各种写法都能用。除了为<code>10</code>加上括号,还可以在<code>10</code>后面加两个点,JavaScript 会把第一个点理解成小数点(即<code>10.0</code>),把第二个点理解成调用对象属性,从而得到正确结果。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token number">10.</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
  998. <span class="token comment">// &quot;1010&quot;</span>
  999. <span class="token comment">// 其他方法还包括</span>
  1000. <span class="token number">10</span> <span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1010&quot;</span>
  1001. <span class="token number">10.0</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1010&quot;</span>
  1002. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>这实际上意味着,可以直接对一个小数使用<code>toString</code>方法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token number">10.5</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;10.5&quot;</span>
  1003. <span class="token number">10.5</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1010.1&quot;</span>
  1004. <span class="token number">10.5</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span> <span class="token comment">// &quot;12.4&quot;</span>
  1005. <span class="token number">10.5</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span> <span class="token comment">// &quot;a.8&quot;</span>
  1006. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>通过方括号运算符也可以调用<code>toString</code>方法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token number">10</span><span class="token punctuation">[</span><span class="token string">'toString'</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1010&quot;</span>
  1007. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>toString</code>方法只能将十进制的数,转为其他进制的字符串。如果要将其他进制的数,转回十进制,需要使用<code>parseInt</code>方法。</p> <h3 id="_3-2-number-prototype-tofixed-转成保留指定小数位数的字符串"><a href="#_3-2-number-prototype-tofixed-转成保留指定小数位数的字符串" class="header-anchor">#</a> 3.2 Number.prototype.toFixed() 转成保留指定小数位数的字符串</h3> <p><code>toFixed()</code>方法先将一个数转为指定位数的小数,然后返回这个小数对应的字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;10.00&quot;</span>
  1008. <span class="token number">10.005</span><span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;10.01&quot;</span>
  1009. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,<code>10</code>和<code>10.005</code>先转成2位小数,然后转成字符串。其中<code>10</code>必须放在括号里,否则后面的点会被处理成小数点。</p> <p><code>toFixed()</code>方法的参数为小数位数,有效范围为0到20,超出这个范围将抛出 RangeError 错误。</p> <p>由于浮点数的原因,小数<code>5</code>的四舍五入是不确定的,使用的时候必须小心。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">10.055</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// 10.05</span>
  1010. <span class="token punctuation">(</span><span class="token number">10.005</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toFixed</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// 10.01</span>
  1011. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_3-3-number-prototype-toexponential-转成科学计数法形式字符串"><a href="#_3-3-number-prototype-toexponential-转成科学计数法形式字符串" class="header-anchor">#</a> 3.3 Number.prototype.toExponential() 转成科学计数法形式字符串</h3> <p><code>toExponential</code>方法用于将一个数转为科学计数法形式。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toExponential</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1e+1&quot;</span>
  1012. <span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toExponential</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1.0e+1&quot;</span>
  1013. <span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toExponential</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1.00e+1&quot;</span>
  1014. <span class="token punctuation">(</span><span class="token number">1234</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toExponential</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1.234e+3&quot;</span>
  1015. <span class="token punctuation">(</span><span class="token number">1234</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toExponential</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1.2e+3&quot;</span>
  1016. <span class="token punctuation">(</span><span class="token number">1234</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toExponential</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1.23e+3&quot;</span>
  1017. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><code>toExponential</code>方法的参数是小数点后有效数字的位数,范围为0到20,超出这个范围,会抛出一个 RangeError 错误。</p> <h3 id="_3-4-number-prototype-toprecision-转为指定位数的有效数字"><a href="#_3-4-number-prototype-toprecision-转为指定位数的有效数字" class="header-anchor">#</a> 3.4 Number.prototype.toPrecision()转为指定位数的有效数字</h3> <p><code>Number.prototype.toPrecision()</code>方法用于将一个数转为指定位数的有效数字。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">12.34</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toPrecision</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1e+1&quot;</span>
  1018. <span class="token punctuation">(</span><span class="token number">12.34</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toPrecision</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;12&quot;</span>
  1019. <span class="token punctuation">(</span><span class="token number">12.34</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toPrecision</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// &quot;12.3&quot;</span>
  1020. <span class="token punctuation">(</span><span class="token number">12.34</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toPrecision</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// &quot;12.34&quot;</span>
  1021. <span class="token punctuation">(</span><span class="token number">12.34</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toPrecision</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token comment">// &quot;12.340&quot;</span>
  1022. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>该方法的参数为有效数字的位数,范围是1到21,超出这个范围会抛出 RangeError 错误。</p> <p>该方法用于四舍五入时不太可靠,跟浮点数不是精确储存有关。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">12.35</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toPrecision</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// &quot;12.3&quot;</span>
  1023. <span class="token punctuation">(</span><span class="token number">12.25</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toPrecision</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// &quot;12.3&quot;</span>
  1024. <span class="token punctuation">(</span><span class="token number">12.15</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toPrecision</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// &quot;12.2&quot;</span>
  1025. <span class="token punctuation">(</span><span class="token number">12.45</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toPrecision</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// &quot;12.4&quot;</span>
  1026. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="_4-5-number-prototype-tolocalestring"><a href="#_4-5-number-prototype-tolocalestring" class="header-anchor">#</a> 4.5 Number.prototype.toLocaleString()</h3> <p><code>Number.prototype.toLocaleString()</code>方法接受一个地区码作为参数,返回一个字符串,表示当前数字在该地区的当地书写形式。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token string">'zh-Hans-CN-u-nu-hanidec'</span><span class="token punctuation">)</span>
  1027. <span class="token comment">// &quot;一二三&quot;</span>
  1028. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>该方法还可以接受第二个参数配置对象,用来定制指定用途的返回字符串。该对象的<code>style</code>属性指定输出样式,默认值是<code>decimal</code>,表示输出十进制形式。如果值为<code>percent</code>,表示输出百分数。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token string">'zh-Hans-CN'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">style</span><span class="token operator">:</span> <span class="token string">'persent'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1029. <span class="token comment">// &quot;12,300%&quot;</span>
  1030. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>如果<code>style</code>属性的值为<code>currency</code>,则可以搭配<code>currency</code>属性,输出指定格式的货币字符串形式。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token string">'zh-Hans-CN'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">style</span><span class="token operator">:</span> <span class="token string">'currency'</span><span class="token punctuation">,</span> <span class="token literal-property property">currency</span><span class="token operator">:</span> <span class="token string">'CNY'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1031. <span class="token comment">// &quot;¥123.00&quot;</span>
  1032. <span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token string">'de-DE'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">style</span><span class="token operator">:</span> <span class="token string">'currency'</span><span class="token punctuation">,</span> <span class="token literal-property property">currency</span><span class="token operator">:</span> <span class="token string">'EUR'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1033. <span class="token comment">// &quot;123,00 €&quot;</span>
  1034. <span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token string">'en-US'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">style</span><span class="token operator">:</span> <span class="token string">'currency'</span><span class="token punctuation">,</span> <span class="token literal-property property">currency</span><span class="token operator">:</span> <span class="token string">'USD'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1035. <span class="token comment">// &quot;$123.00&quot;</span>
  1036. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>如果<code>Number.prototype.toLocaleString()</code>省略了参数,则由浏览器自行决定如何处理,通常会使用操作系统的地区设定。注意,该方法如果使用浏览器不认识的地区码,会抛出一个错误。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token string">'123'</span><span class="token punctuation">)</span> <span class="token comment">// 出错</span>
  1037. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="应用-添加千位分隔符"><a href="#应用-添加千位分隔符" class="header-anchor">#</a> 应用-添加千位分隔符</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">123456.789</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1038. <span class="token comment">// 输出 &quot;123,456.789&quot;,常用。</span>
  1039. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token number">1234566</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token string">'zh-Hans-CN-u-nu-hanidec'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1040. <span class="token comment">// 输出&quot;一,二三四,五六六&quot;</span>
  1041. <span class="token punctuation">(</span><span class="token number">1234566</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token string">'zh-Hans-CN-u-nu-hanidec'</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token literal-property property">useGrouping</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1042. <span class="token comment">// 给.toLocaleString()加第二个参数,依然输出&quot;一,二三四,五六六&quot;</span>
  1043. <span class="token comment">// 不带千位分隔符</span>
  1044. <span class="token punctuation">(</span><span class="token number">1234566</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token string">'zh-Hans-CN-u-nu-hanidec'</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token literal-property property">useGrouping</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1045. <span class="token comment">// 输出&quot;一二三四五六六&quot;,这个比较有用,也就是最简单的将阿拉伯数字变成汉字的办法。</span>
  1046. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="_4、自定义方法-2"><a href="#_4、自定义方法-2" class="header-anchor">#</a> 4、自定义方法</h2> <p>与其他对象一样,<code>Number.prototype</code>对象上面可以自定义方法,被<code>Number</code>的实例继承。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token class-name">Number</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">add</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">x</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1047. <span class="token keyword">return</span> <span class="token keyword">this</span> <span class="token operator">+</span> x<span class="token punctuation">;</span>
  1048. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  1049. <span class="token number">8</span><span class="token punctuation">[</span><span class="token string">'add'</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// 10</span>
  1050. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码为<code>Number</code>对象实例定义了一个<code>add</code>方法。在数值上调用某个方法,数值会自动转为<code>Number</code>的实例对象,所以就可以调用<code>add</code>方法了。由于<code>add</code>方法返回的还是数值,所以可以链式运算。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token class-name">Number</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">subtract</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">x</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1051. <span class="token keyword">return</span> <span class="token keyword">this</span> <span class="token operator">-</span> x<span class="token punctuation">;</span>
  1052. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  1053. <span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">subtract</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>
  1054. <span class="token comment">// 6</span>
  1055. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码在<code>Number</code>对象的实例上部署了<code>subtract</code>方法,它可以与<code>add</code>方法链式调用。</p> <p>我们还可以部署更复杂的方法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token class-name">Number</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">iterate</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1056. <span class="token keyword">var</span> result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  1057. <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token keyword">this</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1058. result<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1059. <span class="token punctuation">}</span>
  1060. <span class="token keyword">return</span> result<span class="token punctuation">;</span>
  1061. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  1062. <span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterate</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1063. <span class="token comment">// [0, 1, 2, 3, 4, 5, 6, 7, 8]</span>
  1064. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>上面代码在<code>Number</code>对象的原型上部署了<code>iterate</code>方法,将一个数值自动遍历为一个数组。</p> <p><strong>注意</strong>,数值的自定义方法,只能定义在它的原型对象<code>Number.prototype</code>上面,数值本身是无法自定义属性的。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
  1065. n<span class="token punctuation">.</span>x <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
  1066. n<span class="token punctuation">.</span>x <span class="token comment">// undefined</span>
  1067. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,<code>n</code>是一个原始类型的数值。直接在它上面新增一个属性<code>x</code>,不会报错,但毫无作用,总是返回<code>undefined</code>。这是因为一旦被调用属性,<code>n</code>就自动转为<code>Number</code>的实例对象,调用结束后,该对象自动销毁。所以,下一次调用<code>n</code>的属性时,实际取到的是另一个对象,属性<code>x</code>当然就读不出来。</p> <h2 id="七、string对象"><a href="#七、string对象" class="header-anchor">#</a> 七、String对象</h2> <h2 id="_1、概述-5"><a href="#_1、概述-5" class="header-anchor">#</a> 1、概述</h2> <p><code>String</code>对象是 JavaScript 原生提供的三个包装对象之一,用来生成字符串对象。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> s1 <span class="token operator">=</span> <span class="token string">'abc'</span><span class="token punctuation">;</span>
  1068. <span class="token keyword">var</span> s2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 使用new关键字会变成包装对象</span>
  1069. <span class="token keyword">typeof</span> s1 <span class="token comment">// &quot;string&quot;</span>
  1070. <span class="token keyword">typeof</span> s2 <span class="token comment">// &quot;object&quot;</span>
  1071. s2<span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;abc&quot;</span>
  1072. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中,变量<code>s1</code>是字符串,<code>s2</code>是对象。由于<code>s2</code>是字符串对象,<code>s2.valueOf</code>方法返回的就是它所对应的原始字符串。</p> <p><strong>字符串对象是一个类似数组的对象</strong>(很像数组,但不是数组)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> strObj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span>
  1073. <span class="token comment">// String {0: &quot;a&quot;, 1: &quot;b&quot;, 2: &quot;c&quot;, length: 3}</span>
  1074. strObj<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment">// &quot;b&quot;</span>
  1075. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,字符串<code>abc</code>对应的字符串对象,有数值键(<code>0</code>、<code>1</code>、<code>2</code>)和<code>length</code>属性,所以可以像数组那样取值。</p> <p>除了用作构造函数,<code>String</code>对象还可以当作工具方法使用,将任意类型的值转为字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">String</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token comment">// &quot;true&quot;</span>
  1076. <span class="token function">String</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token comment">// &quot;false&quot;</span>
  1077. <span class="token function">String</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token comment">// &quot;5&quot;</span>
  1078. <span class="token function">String</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token comment">// 'null'</span>
  1079. <span class="token function">String</span><span class="token punctuation">(</span><span class="token keyword">undefined</span><span class="token punctuation">)</span> <span class="token comment">//'undefined'</span>
  1080. <span class="token function">String</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// '[object Object]'</span>
  1081. <span class="token function">String</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// ''</span>
  1082. <span class="token function">String</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'1'</span><span class="token punctuation">,</span><span class="token string">'2'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// '1,2'</span>
  1083. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码将各种类型的值,分别转换为字符串。</p> <h2 id="_2、静态方法-2"><a href="#_2、静态方法-2" class="header-anchor">#</a> 2、静态方法</h2> <h3 id="string-fromcharcode"><a href="#string-fromcharcode" class="header-anchor">#</a> String.fromCharCode()</h3> <p><code>String</code>对象提供的静态方法(即定义在对象本身,而不是定义在对象实例的方法),主要是<code>String.fromCharCode()</code>。<strong>该方法的参数是一个或多个数值,代表 Unicode 码点,返回值是这些码点组成的字符串。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>String<span class="token punctuation">.</span><span class="token function">fromCharCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;&quot;</span>
  1084. String<span class="token punctuation">.</span><span class="token function">fromCharCode</span><span class="token punctuation">(</span><span class="token number">97</span><span class="token punctuation">)</span> <span class="token comment">// &quot;a&quot;</span>
  1085. String<span class="token punctuation">.</span><span class="token function">fromCharCode</span><span class="token punctuation">(</span><span class="token number">104</span><span class="token punctuation">,</span> <span class="token number">101</span><span class="token punctuation">,</span> <span class="token number">108</span><span class="token punctuation">,</span> <span class="token number">108</span><span class="token punctuation">,</span> <span class="token number">111</span><span class="token punctuation">)</span>
  1086. <span class="token comment">// &quot;hello&quot;</span>
  1087. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,<code>String.fromCharCode</code>方法的参数为空,就返回空字符串;否则,返回参数对应的 Unicode 字符串。</p> <p>注意,该方法不支持 Unicode 码点大于<code>0xFFFF</code>的字符,即传入的参数不能大于<code>0xFFFF</code>(即十进制的 65535)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>String<span class="token punctuation">.</span><span class="token function">fromCharCode</span><span class="token punctuation">(</span><span class="token number">0x20BB7</span><span class="token punctuation">)</span>
  1088. <span class="token comment">// &quot;ஷ&quot;</span>
  1089. String<span class="token punctuation">.</span><span class="token function">fromCharCode</span><span class="token punctuation">(</span><span class="token number">0x20BB7</span><span class="token punctuation">)</span> <span class="token operator">===</span> String<span class="token punctuation">.</span><span class="token function">fromCharCode</span><span class="token punctuation">(</span><span class="token number">0x0BB7</span><span class="token punctuation">)</span>
  1090. <span class="token comment">// true</span>
  1091. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,<code>String.fromCharCode</code>参数<code>0x20BB7</code>大于<code>0xFFFF</code>,导致返回结果出错。<code>0x20BB7</code>对应的字符是汉字<code>𠮷</code>,但是返回结果却是另一个字符(码点<code>0x0BB7</code>)。这是因为<code>String.fromCharCode</code>发现参数值大于<code>0xFFFF</code>,就会忽略多出的位(即忽略<code>0x20BB7</code>里面的<code>2</code>)。</p> <p>这种现象的根本原因在于,码点大于<code>0xFFFF</code>的字符占用四个字节,而 JavaScript 默认支持两个字节的字符。这种情况下,必须把<code>0x20BB7</code>拆成两个字符表示。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>String<span class="token punctuation">.</span><span class="token function">fromCharCode</span><span class="token punctuation">(</span><span class="token number">0xD842</span><span class="token punctuation">,</span> <span class="token number">0xDFB7</span><span class="token punctuation">)</span>
  1092. <span class="token comment">// &quot;𠮷&quot;</span>
  1093. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,<code>0x20BB7</code>拆成两个字符<code>0xD842</code>和<code>0xDFB7</code>(即两个两字节字符,合成一个四字节字符),就能得到正确的结果。码点大于<code>0xFFFF</code>的字符的四字节表示法,由 UTF-16 编码方法决定。</p> <h2 id="_3、实例属性"><a href="#_3、实例属性" class="header-anchor">#</a> 3、实例属性</h2> <h3 id="string-prototype-length"><a href="#string-prototype-length" class="header-anchor">#</a> String.prototype.length</h3> <p>字符串实例的<code>length</code>属性返回字符串的长度。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'abc'</span><span class="token punctuation">.</span>length <span class="token comment">// 3</span>
  1094. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_4、实例方法"><a href="#_4、实例方法" class="header-anchor">#</a> 4、实例方法</h2> <h3 id="_4-1-string-prototype-charat-返回指定位置的字符"><a href="#_4-1-string-prototype-charat-返回指定位置的字符" class="header-anchor">#</a> 4.1 String.prototype.charAt() 返回指定位置的字符</h3> <p><code>charAt</code>方法<strong>返回指定位置的字符</strong>,参数是从<code>0</code>开始编号的位置。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1095. s<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;b&quot;</span>
  1096. s<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;c&quot;</span>
  1097. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>这个方法完全可以用数组下标替代。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'abc'</span><span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;b&quot;</span>
  1098. <span class="token string">'abc'</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment">// &quot;b&quot;</span>
  1099. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>如果参数为负数,或大于等于字符串的长度,<code>charAt</code>返回空字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'abc'</span><span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;&quot;</span>
  1100. <span class="token string">'abc'</span><span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// &quot;&quot;</span>
  1101. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_4-2-string-prototype-charcodeat-返回字符串指定位置的-unicode-码点-十进制表示"><a href="#_4-2-string-prototype-charcodeat-返回字符串指定位置的-unicode-码点-十进制表示" class="header-anchor">#</a> 4.2 String.prototype.charCodeAt()返回字符串指定位置的 Unicode 码点(十进制表示)</h3> <p><code>charCodeAt</code>方法<strong>返回字符串指定位置的 Unicode 码点(十进制表示)</strong>,相当于<code>String.fromCharCode()</code>的逆操作。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'abc'</span><span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 98</span>
  1102. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中,<code>abc</code>的<code>1</code>号位置的字符是<code>b</code>,它的 Unicode 码点是<code>98</code>。</p> <p>如果没有任何参数,<code>charCodeAt</code>返回首字符的 Unicode 码点。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'abc'</span><span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 97</span>
  1103. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果参数为负数,或大于等于字符串的长度,<code>charCodeAt</code>返回<code>NaN</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'abc'</span><span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// NaN</span>
  1104. <span class="token string">'abc'</span><span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// NaN</span>
  1105. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>注意</strong>,<code>charCodeAt</code>方法返回的 Unicode 码点不会大于65536(0xFFFF),也就是说,只返回两个字节的字符的码点。如果遇到码点大于 65536 的字符(四个字节的字符),必需连续使用两次<code>charCodeAt</code>,不仅读入<code>charCodeAt(i)</code>,还要读入<code>charCodeAt(i+1)</code>,将两个值放在一起,才能得到准确的字符。</p> <h3 id="_4-3-string-prototype-concat-合并字符串-返回新字符串-不改变原字符串"><a href="#_4-3-string-prototype-concat-合并字符串-返回新字符串-不改变原字符串" class="header-anchor">#</a> 4.3 String.prototype.concat() 合并字符串,返回新字符串,不改变原字符串</h3> <p><code>concat</code>方法<strong>用于连接两个字符串,返回一个新字符串,不改变原字符串</strong>。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>var s1 = 'abc';
  1106. var s2 = 'def';
  1107. s1.concat(s2) // &quot;abcdef&quot;
  1108. s1 // &quot;abc&quot;
  1109. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>该方法可以接受多个参数。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>'a'.concat('b', 'c') // &quot;abc&quot;
  1110. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果参数不是字符串,<code>concat</code>方法会将其先转为字符串,然后再连接。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>var one = 1;
  1111. var two = 2;
  1112. var three = '3';
  1113. ''.concat(one, two, three) // &quot;123&quot;
  1114. one + two + three // &quot;33&quot;
  1115. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,<code>concat</code>方法将参数先转成字符串再连接,所以返回的是一个三个字符的字符串。作为对比,加号运算符在两个运算数都是数值时,不会转换类型,所以返回的是一个两个字符的字符串。</p> <h3 id="_4-4-string-prototype-slice-取出部分子字符串-不改变原字符串"><a href="#_4-4-string-prototype-slice-取出部分子字符串-不改变原字符串" class="header-anchor">#</a> 4.4 String.prototype.slice() 取出部分子字符串,不改变原字符串</h3> <p><code>slice</code>方法<strong>用于从原字符串取出子字符串并返回,不改变原字符串</strong>。它的第一个参数是子字符串的<strong>开始位置</strong>,第二个参数是子字符串的<strong>结束位置(不含该位置)</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Java&quot;</span>
  1116. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果省略第二个参数,则表示子字符串一直到原字符串结束。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Script&quot; 省略参数二,会取到结束</span>
  1117. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果参数是负值,表示从结尾开始倒数计算的位置,即该负值加上字符串长度。</p> <h5 id="负数表示倒数"><a href="#负数表示倒数" class="header-anchor">#</a> 负数表示倒数</h5> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Script&quot;</span>
  1118. <span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Java&quot;</span>
  1119. <span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;p&quot;</span>
  1120. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>如果第一个参数大于第二个参数,<code>slice</code>方法返回一个空字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;&quot;</span>
  1121. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_4-5-string-prototype-substring-和slice类似"><a href="#_4-5-string-prototype-substring-和slice类似" class="header-anchor">#</a> 4.5 String.prototype.substring() 和slice类似</h3> <p><code>substring</code>方法<strong>用于从原字符串取出子字符串并返回,不改变原字符串</strong>,跟<code>slice</code>方法很相像。它的第一个参数表示子字符串的<strong>开始位置</strong>,第二个位置表示<strong>结束位置(返回结果不含该位置)</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Java&quot;</span>
  1122. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果省略第二个参数,则表示子字符串一直到原字符串的结束。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Script&quot; 省略参数二,会取到结束</span>
  1123. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果第一个参数大于第二个参数,<code>substring</code>方法会自动更换两个参数的位置。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Script&quot; // substring自动更换位置,slice则是返回空字符</span>
  1124. <span class="token comment">// 等同于</span>
  1125. <span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Script&quot;</span>
  1126. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,调换<code>substring</code>方法的两个参数,都得到同样的结果。</p> <p>如果参数是负数,<code>substring</code>方法会自动将负数转为0。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// &quot;JavaScript&quot; // substring负数自动转为0,slice负数表示倒数</span>
  1127. <span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Java&quot;</span>
  1128. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,第二个例子的参数<code>-3</code>会自动变成<code>0</code>,等同于<code>'JavaScript'.substring(4, 0)</code>。由于第二个参数小于第一个参数,会自动互换位置,所以返回<code>Java</code>。</p> <p>由于这些规则违反直觉,因此<strong>不建议使用<code>substring</code>方法,应该优先使用<code>slice</code></strong>。</p> <h4 id="小总结-substring-和-slice-的区别-有两个。"><a href="#小总结-substring-和-slice-的区别-有两个。" class="header-anchor">#</a> 小总结:substring() 和 slice() 的区别,有两个。</h4> <blockquote><ul><li>负数参数:substring负数自动转为0,slice负数表示倒数</li> <li>参数一大于参数二:substring自动更换位置,slice则是返回空字符</li></ul></blockquote> <h3 id="_4-6-string-prototype-substr-和-slice、substring类似-区别是参数二表示长度"><a href="#_4-6-string-prototype-substr-和-slice、substring类似-区别是参数二表示长度" class="header-anchor">#</a> 4.6 String.prototype.substr() 和 slice、substring类似,区别是参数二表示长度</h3> <p><code>substr</code>方法用于<strong>从原字符串取出子字符串并返回,不改变原字符串</strong>,跟<code>slice</code>和<code>substring</code>方法的作用相同。</p> <p><code>substr</code>方法的第一个参数是子字符串的开始位置(从0开始计算),<strong>第二个参数是子字符串的长度</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Script&quot;</span>
  1129. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果省略第二个参数,则表示子字符串一直到原字符串的结束。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Script&quot; 省略参数二,会取到结束</span>
  1130. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果第一个参数是负数,表示倒数计算的字符位置。如果第二个参数是负数,将被自动转为0,因此会返回空字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Script&quot; 参数一是负数表示倒数</span>
  1131. <span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;&quot; 参数二是负数转为0</span>
  1132. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,第二个例子的参数<code>-1</code>自动转为<code>0</code>,表示子字符串长度为<code>0</code>,所以返回空字符串。</p> <h3 id="_4-7-string-prototype-indexof-搜索子串第一个出现的位置-string-prototype-lastindexof-搜索子串最后出现的位置"><a href="#_4-7-string-prototype-indexof-搜索子串第一个出现的位置-string-prototype-lastindexof-搜索子串最后出现的位置" class="header-anchor">#</a> 4.7 String.prototype.indexOf() 搜索子串第一个出现的位置,String.prototype.lastIndexOf() 搜索子串最后出现的位置</h3> <p><code>indexOf</code>方法<strong>用于确定一个字符串在另一个字符串中第一次出现的位置</strong>,<strong>返回结果是匹配开始的位置</strong>。如果返回<code>-1</code>,就表示不匹配。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'hello world'</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'o'</span><span class="token punctuation">)</span> <span class="token comment">// 4</span>
  1133. <span class="token string">'JavaScript'</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'script'</span><span class="token punctuation">)</span> <span class="token comment">// -1</span>
  1134. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>indexOf</code>方法还可以接受第二个参数,表示从该位置开始向后匹配。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'hello world'</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'o'</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment">// 7</span>
  1135. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>lastIndexOf</code>方法的用法跟<code>indexOf</code>方法一致,主要的区别是<code>lastIndexOf</code>从尾部开始匹配,<code>indexOf</code>则是从头部开始匹配。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'hello world'</span><span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">'o'</span><span class="token punctuation">)</span> <span class="token comment">// 7</span>
  1136. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>另外,<code>lastIndexOf</code>的第二个参数表示从该位置起向前匹配。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'hello world'</span><span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">'o'</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment">// 4</span>
  1137. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_4-8-string-prototype-trim-修剪-两端空格-不改变原字符串"><a href="#_4-8-string-prototype-trim-修剪-两端空格-不改变原字符串" class="header-anchor">#</a> 4.8 String.prototype.trim() ''修剪'‘两端空格,不改变原字符串</h3> <p><code>trim</code>方法<strong>用于去除字符串两端的空格,返回一个新字符串,不改变原字符串。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">' hello world '</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1138. <span class="token comment">// &quot;hello world&quot;</span>
  1139. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>该方法去除的不仅是空格,还包括<strong>制表符(<code>\t</code>、<code>\v</code>)、换行符(<code>\n</code>)和回车符(<code>\r</code>)</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'\r\nabc \t'</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 'abc'</span>
  1140. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_4-9-string-prototype-tolowercase-全转小写-string-prototype-touppercase-全转大写-都不改变原字符串"><a href="#_4-9-string-prototype-tolowercase-全转小写-string-prototype-touppercase-全转大写-都不改变原字符串" class="header-anchor">#</a> 4.9 String.prototype.toLowerCase() 全转小写,String.prototype.toUpperCase() 全转大写,都不改变原字符串</h3> <p><code>toLowerCase</code>方法用于将一个字符串<strong>全部转为小写</strong>,<code>toUpperCase</code>则是<strong>全部转为大写</strong>。它们都<strong>返回一个新字符串,不改变原字符串</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'Hello World'</span><span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1141. <span class="token comment">// &quot;hello world&quot;</span>
  1142. <span class="token string">'Hello World'</span><span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1143. <span class="token comment">// &quot;HELLO WORLD&quot;</span>
  1144. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_4-10-string-prototype-match-匹配子串-返回数组-数组带index、input属性"><a href="#_4-10-string-prototype-match-匹配子串-返回数组-数组带index、input属性" class="header-anchor">#</a> 4.10 String.prototype.match() 匹配子串,返回数组(数组带index、input属性)</h3> <p><code>match</code>方法<strong>用于确定原字符串是否匹配某个子字符串,返回一个数组,成员为匹配的第一个字符串</strong>。如果没有找到匹配,则返回<code>null</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'cat, bat, sat, fat'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string">'at'</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;at&quot;]</span>
  1145. <span class="token comment">// [&quot;at&quot;, index: 1, input: &quot;cat, bat, sat, fat&quot;, groups: undefined]</span>
  1146. <span class="token string">'catbatsatbat'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string">'bat'</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;bat&quot;]</span>
  1147. <span class="token comment">//[&quot;bat&quot;, index: 3, input: &quot;catbatsatbat&quot;, groups: undefined]</span>
  1148. <span class="token comment">// index属性为bat首次出现的索引</span>
  1149. <span class="token string">'cat, bat, sat, fat'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string">'xt'</span><span class="token punctuation">)</span> <span class="token comment">// null</span>
  1150. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>返回的数组还有**<code>index</code>属性<strong>和</strong><code>input</code>属性**,分别表示匹配字符串<strong>开始的位置</strong>和<strong>原始字符串</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> matches <span class="token operator">=</span> <span class="token string">'cat, bat, sat, fat'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string">'at'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1151. matches<span class="token punctuation">.</span>index <span class="token comment">// 1 子串首次出现的索引位置</span>
  1152. matches<span class="token punctuation">.</span>input <span class="token comment">// &quot;cat, bat, sat, fat&quot;</span>
  1153. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>match</code>方法还可以使用<strong>正则表达式</strong>作为参数,详见《正则表达式》一章。</p> <h3 id="_4-11-string-prototype-search-搜索子串第一次出现的位置-没有则返-1-string-prototype-replace-替换子串-参数二替换参数一-不改变原字符串"><a href="#_4-11-string-prototype-search-搜索子串第一次出现的位置-没有则返-1-string-prototype-replace-替换子串-参数二替换参数一-不改变原字符串" class="header-anchor">#</a> 4.11 String.prototype.search() 搜索子串第一次出现的位置,没有则返-1,String.prototype.replace() 替换子串,参数二替换参数一,不改变原字符串</h3> <p><code>search</code>方法的用法基本等同于<code>match</code>,但是<strong>返回值为匹配的第一个位置。如果没有找到匹配,则返回<code>-1</code>。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'cat, bat, sat, fat'</span><span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token string">'at'</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  1154. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>search</code>方法还可以使用正则表达式作为参数,详见《正则表达式》一节。</p> <p><code>replace</code>方法<strong>用于替换匹配的子字符串</strong>,一般情况下只替换第一个匹配(除非使用带有<code>g</code>修饰符的正则表达式)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'aaa'</span><span class="token punctuation">;</span>
  1155. str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;baa&quot; 一般情况只替换第一个,除非带有g的正则</span>
  1156. str <span class="token comment">// 'aaa' 不改变原字符串</span>
  1157. str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">'c'</span><span class="token punctuation">,</span><span class="token string">'b'</span><span class="token punctuation">)</span> <span class="token comment">// 'aaa' 匹配不到返回原字符串</span>
  1158. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>replace</code>方法还可以使用正则表达式作为参数,详见《正则表达式》一节。</p> <h3 id="_4-12-string-prototype-split-按给定规则分割字符串-返回数组"><a href="#_4-12-string-prototype-split-按给定规则分割字符串-返回数组" class="header-anchor">#</a> 4.12 String.prototype.split() 按给定规则分割字符串,返回数组</h3> <p><code>split</code>方法<strong>按照给定规则分割字符串,返回一个由分割出来的子字符串组成的数组。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'a|b|c'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'|'</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
  1159. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果分割规则为空字符串,则返回数组的成员是原字符串的每一个字符。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'a|b|c'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;, &quot;|&quot;, &quot;b&quot;, &quot;|&quot;, &quot;c&quot;]</span>
  1160. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果<strong>省略参数,则返回数组的唯一成员就是原字符串</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'a|b|c'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;a|b|c&quot;]</span>
  1161. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果满足分割规则的两个部分紧邻着(即两个分割符中间没有其他字符),则返回数组之中会有一个空字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'a||c'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'|'</span><span class="token punctuation">)</span> <span class="token comment">// ['a', '', 'c']</span>
  1162. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果满足分割规则的部分处于字符串的开头或结尾(即它的前面或后面没有其他字符),则返回数组的第一个或最后一个成员是一个空字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'|b|c'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'|'</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;&quot;, &quot;b&quot;, &quot;c&quot;]</span>
  1163. <span class="token string">'a|b|'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'|'</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;, &quot;b&quot;, &quot;&quot;]</span>
  1164. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>split</code>方法还可以接受<strong>第二个参数</strong>,<strong>限定返回数组的最大成员数(数组长度)</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'a|b|c'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'|'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">// []</span>
  1165. <span class="token string">'a|b|c'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'|'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;]</span>
  1166. <span class="token string">'a|b|c'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'|'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;, &quot;b&quot;]</span>
  1167. <span class="token string">'a|b|c'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'|'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
  1168. <span class="token string">'a|b|c'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'|'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
  1169. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,<code>split</code>方法的第二个参数,决定了返回数组的成员数。</p> <p><code>split</code>方法还可以使用正则表达式作为参数,详见《正则表达式》一节。</p> <h3 id="_4-13-string-prototype-localecompare-比较两个字符串-返回整数。"><a href="#_4-13-string-prototype-localecompare-比较两个字符串-返回整数。" class="header-anchor">#</a> 4.13 String.prototype.localeCompare() 比较两个字符串,返回整数。</h3> <p><code>localeCompare</code>方法 <strong>用于比较两个字符串。它返回一个整数</strong>,<strong>如果小于0,表示第一个字符串小于第二个字符串;如果等于0,表示两者相等;如果大于0,表示第一个字符串大于第二个字符串。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'apple'</span><span class="token punctuation">.</span><span class="token function">localeCompare</span><span class="token punctuation">(</span><span class="token string">'banana'</span><span class="token punctuation">)</span> <span class="token comment">// -1</span>
  1170. <span class="token string">'apple'</span><span class="token punctuation">.</span><span class="token function">localeCompare</span><span class="token punctuation">(</span><span class="token string">'apple'</span><span class="token punctuation">)</span> <span class="token comment">// 0</span>
  1171. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>该方法的最大特点,就是会考虑自然语言的顺序。举例来说,正常情况下,大写的英文字母小于小写字母。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'B'</span> <span class="token operator">&gt;</span> <span class="token string">'a'</span> <span class="token comment">// false</span>
  1172. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中,字母<code>B</code>小于字母<code>a</code>。因为 JavaScript 采用的是 Unicode 码点比较,<code>B</code>的码点是66,而<code>a</code>的码点是97。</p> <p>但是,<code>localeCompare</code>方法会考虑自然语言的排序情况,将<code>B</code>排在<code>a</code>的前面。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'B'</span><span class="token punctuation">.</span><span class="token function">localeCompare</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  1173. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中,<code>localeCompare</code>方法返回整数1,表示<code>B</code>较大。</p> <p><code>localeCompare</code>还可以有第二个参数,指定所使用的语言(默认是英语),然后根据该语言的规则进行比较。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'ä'</span><span class="token punctuation">.</span><span class="token function">localeCompare</span><span class="token punctuation">(</span><span class="token string">'z'</span><span class="token punctuation">,</span> <span class="token string">'de'</span><span class="token punctuation">)</span> <span class="token comment">// -1</span>
  1174. <span class="token string">'ä'</span><span class="token punctuation">.</span><span class="token function">localeCompare</span><span class="token punctuation">(</span><span class="token string">'z'</span><span class="token punctuation">,</span> <span class="token string">'sv'</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  1175. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,<code>de</code>表示德语,<code>sv</code>表示瑞典语。德语中,<code>ä</code>小于<code>z</code>,所以返回<code>-1</code>;瑞典语中,<code>ä</code>大于<code>z</code>,所以返回<code>1</code>。</p> <h2 id="八、math对象"><a href="#八、math对象" class="header-anchor">#</a> 八、Math对象</h2> <p><code>Math</code>是 JavaScript 的原生对象,提供各种<strong>数学</strong>功能。该对象<strong>不是构造函数,不能生成实例</strong>,所有的属性和方法都必须在<code>Math</code>对象上调用。</p> <h2 id="_1、静态属性"><a href="#_1、静态属性" class="header-anchor">#</a> 1、静态属性</h2> <p><code>Math</code>对象的静态属性,提供以下一些数学常数。</p> <ul><li><code>Math.E</code>:常数<code>e</code>。</li> <li><code>Math.LN2</code>:2 的自然对数。</li> <li><code>Math.LN10</code>:10 的自然对数。</li> <li><code>Math.LOG2E</code>:以 2 为底的<code>e</code>的对数。</li> <li><code>Math.LOG10E</code>:以 10 为底的<code>e</code>的对数。</li> <li><code>Math.PI</code>:常数<code>π</code>。</li> <li><code>Math.SQRT1_2</code>:0.5 的平方根。</li> <li><code>Math.SQRT2</code>:2 的平方根。</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token constant">E</span> <span class="token comment">// 2.718281828459045</span>
  1176. Math<span class="token punctuation">.</span><span class="token constant">LN2</span> <span class="token comment">// 0.6931471805599453</span>
  1177. Math<span class="token punctuation">.</span><span class="token constant">LN10</span> <span class="token comment">// 2.302585092994046</span>
  1178. Math<span class="token punctuation">.</span><span class="token constant">LOG2E</span> <span class="token comment">// 1.4426950408889634</span>
  1179. Math<span class="token punctuation">.</span><span class="token constant">LOG10E</span> <span class="token comment">// 0.4342944819032518</span>
  1180. Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token comment">// 3.141592653589793</span>
  1181. Math<span class="token punctuation">.</span><span class="token constant">SQRT1_2</span> <span class="token comment">// 0.7071067811865476</span>
  1182. Math<span class="token punctuation">.</span><span class="token constant">SQRT2</span> <span class="token comment">// 1.4142135623730951</span>
  1183. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>这些属性都是<strong>只读的</strong>,不能修改。</p> <h2 id="_2、静态方法-3"><a href="#_2、静态方法-3" class="header-anchor">#</a> 2、静态方法</h2> <p><code>Math</code>对象提供以下一些静态方法。</p> <ul><li><code>Math.abs()</code>:绝对值</li> <li><code>Math.ceil()</code>:向上取整</li> <li><code>Math.floor()</code>:向下取整</li> <li><code>Math.max()</code>:最大值</li> <li><code>Math.min()</code>:最小值</li> <li><code>Math.pow()</code>:指数运算</li> <li><code>Math.sqrt()</code>:平方根</li> <li><code>Math.log()</code>:自然对数</li> <li><code>Math.exp()</code>:<code>e</code>的指数</li> <li><code>Math.round()</code>:四舍五入</li> <li><code>Math.random()</code>:随机数</li></ul> <h3 id="_2-1-math-abs-绝对值"><a href="#_2-1-math-abs-绝对值" class="header-anchor">#</a> 2.1 Math.abs() 绝对值</h3> <p><code>Math.abs</code>方法<strong>返回参数值的绝对值</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  1184. Math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  1185. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_2-2-math-max-最大值-math-min-最-小值"><a href="#_2-2-math-max-最大值-math-min-最-小值" class="header-anchor">#</a> 2.2 Math.max() 最大值,Math.min()最 小值</h3> <p><code>Math.max</code>方法<strong>返回参数之中最大的那个值</strong>,<code>Math.min</code><strong>返回最小的那个值</strong>。如果参数为空, <code>Math.min</code>返回<code>Infinity</code>, <code>Math.max</code>返回<code>-Infinity</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token comment">// 5</span>
  1186. Math<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token comment">// -1</span>
  1187. Math<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// Infinity</span>
  1188. Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// -Infinity</span>
  1189. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="_2-3-math-floor-向下取整-math-ceil-向上取整"><a href="#_2-3-math-floor-向下取整-math-ceil-向上取整" class="header-anchor">#</a> 2.3 Math.floor() 向下取整,Math.ceil() 向上取整</h3> <p><code>Math.floor</code>方法<strong>返回小于参数值的最大整数(地板值)</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span><span class="token number">3.2</span><span class="token punctuation">)</span> <span class="token comment">// 3</span>
  1190. Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3.2</span><span class="token punctuation">)</span> <span class="token comment">// -4</span>
  1191. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>Math.ceil</code>方法<strong>返回大于参数值的最小整数(天花板值)</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span><span class="token number">3.2</span><span class="token punctuation">)</span> <span class="token comment">// 4</span>
  1192. Math<span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3.2</span><span class="token punctuation">)</span> <span class="token comment">// -3</span>
  1193. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>这两个方法可以结合起来,实现一个<strong>总是返回数值的整数部分的函数</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">ToInteger</span><span class="token punctuation">(</span><span class="token parameter">x</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1194. x <span class="token operator">=</span> <span class="token function">Number</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1195. <span class="token keyword">return</span> x <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token operator">?</span> Math<span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token operator">:</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1196. <span class="token punctuation">}</span>
  1197. <span class="token function">ToInteger</span><span class="token punctuation">(</span><span class="token number">3.2</span><span class="token punctuation">)</span> <span class="token comment">// 3</span>
  1198. <span class="token function">ToInteger</span><span class="token punctuation">(</span><span class="token number">3.5</span><span class="token punctuation">)</span> <span class="token comment">// 3</span>
  1199. <span class="token function">ToInteger</span><span class="token punctuation">(</span><span class="token number">3.8</span><span class="token punctuation">)</span> <span class="token comment">// 3</span>
  1200. <span class="token function">ToInteger</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3.2</span><span class="token punctuation">)</span> <span class="token comment">// -3</span>
  1201. <span class="token function">ToInteger</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3.5</span><span class="token punctuation">)</span> <span class="token comment">// -3</span>
  1202. <span class="token function">ToInteger</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3.8</span><span class="token punctuation">)</span> <span class="token comment">// -3</span>
  1203. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,不管正数或负数,<code>ToInteger</code>函数总是返回一个数值的整数部分。</p> <h3 id="_2-4-math-round-四舍五入"><a href="#_2-4-math-round-四舍五入" class="header-anchor">#</a> 2.4 Math.round() 四舍五入</h3> <p><code>Math.round</code>方法用于<strong>四舍五入</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span> <span class="token comment">// 0</span>
  1204. Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  1205. Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token number">0.6</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  1206. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>注意</strong>,它对负数的处理(主要是对<code>.5</code>的处理)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1.1</span><span class="token punctuation">)</span> <span class="token comment">// -1</span>
  1207. Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1.5</span><span class="token punctuation">)</span> <span class="token comment">// -1</span>
  1208. Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1.6</span><span class="token punctuation">)</span> <span class="token comment">// -2</span>
  1209. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_2-5-math-pow-指数运算-次方运算"><a href="#_2-5-math-pow-指数运算-次方运算" class="header-anchor">#</a> 2.5 Math.pow() 指数运算 (次方运算)</h3> <p><code>Math.pow</code>方法返回<strong>以第一个参数为底数、第二个参数为幂的指数值</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 等同于 2 ** 2</span>
  1210. Math<span class="token punctuation">.</span><span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// 4 2的2次方</span>
  1211. <span class="token comment">// 等同于 2 ** 3</span>
  1212. Math<span class="token punctuation">.</span><span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// 8 2的3次方</span>
  1213. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>下面是<strong>计算圆面积的方法</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> radius <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
  1214. <span class="token keyword">var</span> area <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token function">pow</span><span class="token punctuation">(</span>radius<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1256.6370614359173</span>
  1215. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_2-6-math-sqrt-平方根"><a href="#_2-6-math-sqrt-平方根" class="header-anchor">#</a> 2.6 Math.sqrt() 平方根</h3> <p><code>Math.sqrt</code>方法<strong>返回参数值的平方根</strong>。如果参数是一个负值,则返回<code>NaN</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// 2</span>
  1216. Math<span class="token punctuation">.</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// NaN</span>
  1217. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_2-7-math-log-自然对数"><a href="#_2-7-math-log-自然对数" class="header-anchor">#</a> 2.7 Math.log() 自然对数</h3> <p><code>Math.log</code>方法返回以<code>e</code>为底的自然对数值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token constant">E</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  1218. Math<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token comment">// 2.302585092994046</span>
  1219. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>如果要计算以10为底的对数,可以先用<code>Math.log</code>求出自然对数,然后除以<code>Math.LN10</code>;求以2为底的对数,可以除以<code>Math.LN2</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">/</span>Math<span class="token punctuation">.</span><span class="token constant">LN10</span> <span class="token comment">// 2</span>
  1220. Math<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">/</span>Math<span class="token punctuation">.</span><span class="token constant">LN2</span> <span class="token comment">// 3</span>
  1221. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_2-8-math-exp-e的指数"><a href="#_2-8-math-exp-e的指数" class="header-anchor">#</a> 2.8 Math.exp() <code>e</code>的指数</h3> <p><code>Math.exp</code>方法返回常数<code>e</code>的参数次方。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">exp</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 2.718281828459045</span>
  1222. Math<span class="token punctuation">.</span><span class="token function">exp</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// 20.085536923187668</span>
  1223. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_2-9-math-random-0到1的随机数-可能等于0-但一定小于1"><a href="#_2-9-math-random-0到1的随机数-可能等于0-但一定小于1" class="header-anchor">#</a> 2.9 Math.random() 0到1的随机数(可能等于0,但一定小于1)</h3> <p><code>Math.random()</code>返回<strong>0到1之间的一个伪随机数</strong>,可能等于0,但是一定小于1。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 0.7151307314634323</span>
  1224. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>任意范围的随机数生成函数</strong>如下。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">getRandomArbitrary</span><span class="token punctuation">(</span><span class="token parameter">min<span class="token punctuation">,</span> max</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1225. <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>max <span class="token operator">-</span> min<span class="token punctuation">)</span> <span class="token operator">+</span> min<span class="token punctuation">;</span>
  1226. <span class="token punctuation">}</span>
  1227. <span class="token function">getRandomArbitrary</span><span class="token punctuation">(</span><span class="token number">1.5</span><span class="token punctuation">,</span> <span class="token number">6.5</span><span class="token punctuation">)</span>
  1228. <span class="token comment">// 2.4942810038223864</span>
  1229. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>任意范围的随机整数生成函数</strong>如下。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">getRandomInt</span><span class="token punctuation">(</span><span class="token parameter">min<span class="token punctuation">,</span> max</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1230. <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>max <span class="token operator">-</span> min <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> min<span class="token punctuation">;</span>
  1231. <span class="token punctuation">}</span>
  1232. <span class="token function">getRandomInt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment">// 5</span>
  1233. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>返回<strong>随机字符</strong>的例子如下。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">random_str</span><span class="token punctuation">(</span><span class="token parameter">length</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1234. <span class="token keyword">var</span> <span class="token constant">ALPHABET</span> <span class="token operator">=</span> <span class="token string">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span><span class="token punctuation">;</span>
  1235. <span class="token constant">ALPHABET</span> <span class="token operator">+=</span> <span class="token string">'abcdefghijklmnopqrstuvwxyz'</span><span class="token punctuation">;</span>
  1236. <span class="token constant">ALPHABET</span> <span class="token operator">+=</span> <span class="token string">'0123456789-_'</span><span class="token punctuation">;</span>
  1237. <span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>
  1238. <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> length<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1239. <span class="token keyword">var</span> rand <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token constant">ALPHABET</span><span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1240. str <span class="token operator">+=</span> <span class="token constant">ALPHABET</span><span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span>rand<span class="token punctuation">,</span> rand <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1241. <span class="token punctuation">}</span>
  1242. <span class="token keyword">return</span> str<span class="token punctuation">;</span>
  1243. <span class="token punctuation">}</span>
  1244. <span class="token function">random_str</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment">// &quot;NdQKOr&quot; 参数为指定字符长度</span>
  1245. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>上面代码中,<code>random_str</code>函数接受一个整数作为参数,返回变量<code>ALPHABET</code>内的随机字符所组成的指定长度的字符串。</p> <h3 id="_2-10-三角函数方法"><a href="#_2-10-三角函数方法" class="header-anchor">#</a> 2.10 三角函数方法</h3> <p><code>Math</code>对象还提供一系列三角函数方法。</p> <ul><li><code>Math.sin()</code>:返回参数的正弦(参数为弧度值)</li> <li><code>Math.cos()</code>:返回参数的余弦(参数为弧度值)</li> <li><code>Math.tan()</code>:返回参数的正切(参数为弧度值)</li> <li><code>Math.asin()</code>:返回参数的反正弦(返回值为弧度值)</li> <li><code>Math.acos()</code>:返回参数的反余弦(返回值为弧度值)</li> <li><code>Math.atan()</code>:返回参数的反正切(返回值为弧度值)</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Math<span class="token punctuation">.</span><span class="token function">sin</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">// 0</span>
  1246. Math<span class="token punctuation">.</span><span class="token function">cos</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  1247. Math<span class="token punctuation">.</span><span class="token function">tan</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">// 0</span>
  1248. Math<span class="token punctuation">.</span><span class="token function">sin</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// 1</span>
  1249. Math<span class="token punctuation">.</span><span class="token function">asin</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 1.5707963267948966</span>
  1250. Math<span class="token punctuation">.</span><span class="token function">acos</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 0</span>
  1251. Math<span class="token punctuation">.</span><span class="token function">atan</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 0.7853981633974483</span>
  1252. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>三角函数的应用相关知识:&lt; https://www.w3cplus.com/javascript/trigonometry-you-must-know.html &gt;</p> <h2 id="九、date对象"><a href="#九、date对象" class="header-anchor">#</a> 九、Date对象</h2> <p><code>Date</code>对象是 JavaScript 原生的时间库。它以国际标准时间(UTC)1970年1月1日00:00:00作为时间的零点,可以表示的时间范围是前后各1亿天(单位为毫秒)。</p> <h2 id="_1、普通函数的用法"><a href="#_1、普通函数的用法" class="header-anchor">#</a> 1、普通函数的用法</h2> <p><code>Date</code>对象可以作为普通函数直接调用,返回一个代表当前时间的<strong>字符串</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1253. <span class="token comment">// &quot;Tue Dec 01 2015 09:34:43 GMT+0800 (CST)&quot; 字符串类型</span>
  1254. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>注意</strong>,即使带有参数,<code>Date</code>作为普通函数使用时,返回的还是当前时间。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">Date</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 作为普通函数使用时带有参数返回的也是当前时间</span>
  1255. <span class="token comment">// &quot;Tue Dec 01 2015 09:34:43 GMT+0800 (CST)&quot;</span>
  1256. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码说明,<strong>无论有没有参数,直接调用<code>Date</code>总是返回当前时间</strong>。</p> <h2 id="_2、构造函数的用法"><a href="#_2、构造函数的用法" class="header-anchor">#</a> 2、构造函数的用法</h2> <p><code>Date</code>还可以当作构造函数使用。对它使用<code>new</code>命令,会返回一个<code>Date</code>对象的实例。如果不加参数,实例代表的就是当前时间。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> today <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1257. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>Date</code>实例有一个独特的地方。其他对象求值的时候,都是默认调用<code>.valueOf()</code>方法,但是<code>Date</code>实例求值的时候,默认调用的是<code>toString()</code>方法。这导致对<code>Date</code>实例求值,返回的是一个字符串,代表该实例对应的时间。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> today <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1258. today
  1259. <span class="token comment">// &quot;Tue Dec 01 2015 09:34:43 GMT+0800 (CST)&quot;</span>
  1260. <span class="token comment">// 等同于</span>
  1261. today<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1262. <span class="token comment">// &quot;Tue Dec 01 2015 09:34:43 GMT+0800 (CST)&quot;</span>
  1263. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码中,<code>today</code>是<code>Date</code>的实例,<strong>直接求值等同于调用<code>toString</code>方法</strong>。</p> <p>作为构造函数时,<code>Date</code>对象可<strong>以接受多种格式的参数</strong>,返回一个该参数对应的时间实例。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 参数为时间零点开始计算的毫秒数</span>
  1264. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">1378218728000</span><span class="token punctuation">)</span>
  1265. <span class="token comment">// Tue Sep 03 2013 22:32:08 GMT+0800 (CST)</span>
  1266. <span class="token comment">// 参数为日期字符串</span>
  1267. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'January 6, 2013'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1268. <span class="token comment">// Sun Jan 06 2013 00:00:00 GMT+0800 (CST)</span>
  1269. <span class="token comment">// 参数为多个整数,</span>
  1270. <span class="token comment">// 代表年、月、日、小时、分钟、秒、毫秒</span>
  1271. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
  1272. <span class="token comment">// Tue Jan 01 2013 00:00:00 GMT+0800 (CST)</span>
  1273. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>关于<code>Date</code>构造函数的参数,有几点说明。</p> <p><strong>第一点</strong>,参数可以是负整数,代表1970年元旦之前的时间。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1378218728000</span><span class="token punctuation">)</span>
  1274. <span class="token comment">// Fri Apr 30 1926 17:27:52 GMT+0800 (CST)</span>
  1275. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>第二点</strong>,只要是能被<code>Date.parse()</code>方法解析的字符串,都可以当作参数。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'2013-2-15'</span><span class="token punctuation">)</span>
  1276. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'2013/2/15'</span><span class="token punctuation">)</span>
  1277. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'02/15/2013'</span><span class="token punctuation">)</span>
  1278. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'2013-FEB-15'</span><span class="token punctuation">)</span>
  1279. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'FEB, 15, 2013'</span><span class="token punctuation">)</span>
  1280. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'FEB 15, 2013'</span><span class="token punctuation">)</span>
  1281. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'February, 15, 2013'</span><span class="token punctuation">)</span>
  1282. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'February 15, 2013'</span><span class="token punctuation">)</span>
  1283. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'15 Feb 2013'</span><span class="token punctuation">)</span>
  1284. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'15, February, 2013'</span><span class="token punctuation">)</span>
  1285. <span class="token comment">// Fri Feb 15 2013 00:00:00 GMT+0800 (CST)</span>
  1286. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面多种日期字符串的写法,返回的都是同一个时间。</p> <p><strong>第三</strong>,参数为年、月、日等多个整数时,年和月是不能省略的,其他参数都可以省略的。也就是说,这时至少需要两个参数,因为如果只使用“年”这一个参数,<code>Date</code>会将其解释为毫秒数。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">)</span> <span class="token comment">// 只有一个数字的参数会解释为毫秒数</span>
  1287. <span class="token comment">// Thu Jan 01 1970 08:00:02 GMT+0800 (CST)</span>
  1288. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,2013被解释为毫秒数,而不是年份。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
  1289. <span class="token comment">// Tue Jan 01 2013 00:00:00 GMT+0800 (CST)</span>
  1290. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
  1291. <span class="token comment">// Tue Jan 01 2013 00:00:00 GMT+0800 (CST)</span>
  1292. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
  1293. <span class="token comment">// Tue Jan 01 2013 00:00:00 GMT+0800 (CST)</span>
  1294. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
  1295. <span class="token comment">// Tue Jan 01 2013 00:00:00 GMT+0800 (CST)</span>
  1296. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码中,不管有几个参数,返回的都是2013年1月1日零点。</p> <p>最后,各个参数的取值范围如下。</p> <ul><li>年:使用四位数年份,比如<code>2000</code>。如果写成两位数或个位数,则加上<code>1900</code>,即<code>10</code>代表1910年。如果是负数,表示公元前。</li> <li>月:<strong><code>0</code>表示一月,依次类推,<code>11</code>表示12月</strong>。</li> <li>日:<code>1</code>到<code>31</code>。</li> <li>小时:<code>0</code>到<code>23</code>。</li> <li>分钟:<code>0</code>到<code>59</code>。</li> <li>秒:<code>0</code>到<code>59</code></li> <li>毫秒:<code>0</code>到<code>999</code>。</li></ul> <p>注意,月份从<code>0</code>开始计算,但是,天数从<code>1</code>开始计算。另外,除了日期的默认值为<code>1</code>,小时、分钟、秒钟和毫秒的默认值都是<code>0</code>。</p> <p>这些参数如果超出了正常范围,会被自动折算。比如,如果月设为<code>15</code>,就折算为下一年的4月。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">)</span>
  1297. <span class="token comment">// Tue Apr 01 2014 00:00:00 GMT+0800 (CST)</span>
  1298. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
  1299. <span class="token comment">// Mon Dec 31 2012 00:00:00 GMT+0800 (CST)</span>
  1300. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码的第二个例子,日期设为<code>0</code>,就代表上个月的最后一天。</p> <p>参数还可以使用负数,表示扣去的时间。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
  1301. <span class="token comment">// Sat Dec 01 2012 00:00:00 GMT+0800 (CST)</span>
  1302. <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
  1303. <span class="token comment">// Sun Dec 30 2012 00:00:00 GMT+0800 (CST)</span>
  1304. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,分别对月和日使用了负数,表示从基准日扣去相应的时间。</p> <h2 id="_3、日期的运算"><a href="#_3、日期的运算" class="header-anchor">#</a> 3、日期的运算</h2> <p>类型自动转换时,<code>Date</code>实例如果转为数值,则等于对应的毫秒数;如果转为字符串,则等于对应的日期字符串。所以,两个日期实例对象进行减法运算时,返回的是它们间隔的毫秒数;进行加法运算时,返回的是两个字符串连接而成的新字符串。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1305. <span class="token keyword">var</span> d2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1306. d2 <span class="token operator">-</span> d1 <span class="token comment">// 减法运算返回毫秒数</span>
  1307. <span class="token comment">// 2678400000</span>
  1308. d2 <span class="token operator">+</span> d1 <span class="token comment">// 加法运算返回日期字符串</span>
  1309. <span class="token comment">// &quot;Sat Apr 01 2000 00:00:00 GMT+0800 (CST)Wed Mar 01 2000 00:00:00 GMT+0800 (CST)&quot;</span>
  1310. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="_4、静态方法"><a href="#_4、静态方法" class="header-anchor">#</a> 4、静态方法</h2> <h3 id="_4-1-date-now-当前时间戳"><a href="#_4-1-date-now-当前时间戳" class="header-anchor">#</a> 4.1 Date.now() 当前时间戳</h3> <p><code>Date.now</code>方法<strong>返回当前时间距离时间零点(1970年1月1日 00:00:00 UTC)的毫秒数(即时间戳)</strong>,相当于 Unix 时间戳乘以1000。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 1364026285194</span>
  1311. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_4-2-date-parse-解析日期字符串-返回该时间点的时间戳"><a href="#_4-2-date-parse-解析日期字符串-返回该时间点的时间戳" class="header-anchor">#</a> 4.2 Date.parse() 解析日期字符串,返回该时间点的时间戳</h3> <p><code>Date.parse</code>方法用来<strong>解析日期字符串,返回该时间距离时间零点(1970年1月1日 00:00:00)的毫秒数</strong>。</p> <p>日期字符串应该符合 RFC 2822 和 ISO 8061 这两个标准,即<code>YYYY-MM-DDTHH:mm:ss.sssZ</code>格式,其中最后的<code>Z</code>表示时区。但是,其他格式也可以被解析,请看下面的例子。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Date<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'Aug 9, 1995'</span><span class="token punctuation">)</span>
  1312. Date<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'January 26, 2011 13:51:50'</span><span class="token punctuation">)</span>
  1313. Date<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'Mon, 25 Dec 1995 13:30:00 GMT'</span><span class="token punctuation">)</span>
  1314. Date<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'Mon, 25 Dec 1995 13:30:00 +0430'</span><span class="token punctuation">)</span>
  1315. Date<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'2011-10-10'</span><span class="token punctuation">)</span>
  1316. Date<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'2011-10-10T14:48:00'</span><span class="token punctuation">)</span>
  1317. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面的日期字符串都可以解析。</p> <p>如果解析失败,返回<code>NaN</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>Date<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'xxx'</span><span class="token punctuation">)</span> <span class="token comment">// NaN</span>
  1318. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_4-3-date-utc-以年、月、日等变量作为参数-返回该时间点的时间戳"><a href="#_4-3-date-utc-以年、月、日等变量作为参数-返回该时间点的时间戳" class="header-anchor">#</a> 4.3 Date.UTC() 以年、月、日等变量作为参数,返回该时间点的时间戳</h3> <p><code>Date.UTC</code>方法<strong>接受年、月、日等变量作为参数,返回该时间距离时间零点(1970年1月1日 00:00:00 UTC)的毫秒数。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 格式</span>
  1319. Date<span class="token punctuation">.</span><span class="token constant">UTC</span><span class="token punctuation">(</span>year<span class="token punctuation">,</span> month<span class="token punctuation">[</span><span class="token punctuation">,</span> date<span class="token punctuation">[</span><span class="token punctuation">,</span> hrs<span class="token punctuation">[</span><span class="token punctuation">,</span> min<span class="token punctuation">[</span><span class="token punctuation">,</span> sec<span class="token punctuation">[</span><span class="token punctuation">,</span> ms<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  1320. <span class="token comment">// 用法</span>
  1321. Date<span class="token punctuation">.</span><span class="token constant">UTC</span><span class="token punctuation">(</span><span class="token number">2011</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">567</span><span class="token punctuation">)</span>
  1322. <span class="token comment">// 1293847384567</span>
  1323. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>该方法的参数用法与<code>Date</code>构造函数完全一致,比如月从<code>0</code>开始计算,日期从<code>1</code>开始计算。区别在于<code>Date.UTC</code>方法的参数,会被解释为 UTC 时间(世界标准时间),<code>Date</code>构造函数的参数会被解释为当前时区的时间。</p> <h2 id="_5、实例方法"><a href="#_5、实例方法" class="header-anchor">#</a> 5、实例方法</h2> <p><code>Date</code>的实例对象,有几十个自己的方法,除了<code>valueOf</code>和<code>toString</code>,可以分为以下三类。</p> <ul><li><code>to</code>类:从<code>Date</code>对象返回一个字符串,表示指定的时间。</li> <li><code>get</code>类:获取<code>Date</code>对象的日期和时间。</li> <li><code>set</code>类:设置<code>Date</code>对象的日期和时间。</li></ul> <h3 id="_5-1-date-prototype-valueof-返回时间戳-等同于gettime-。-减法运算默认调用"><a href="#_5-1-date-prototype-valueof-返回时间戳-等同于gettime-。-减法运算默认调用" class="header-anchor">#</a> 5.1 Date.prototype.valueOf() 返回时间戳,等同于getTime()。(减法运算默认调用)</h3> <p><code>valueOf</code>方法<strong>返回实例对象距离时间零点(1970年1月1日00:00:00 UTC)对应的毫秒数</strong>,该方法等同于<code>getTime</code>方法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1324. d<span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 1362790014817</span>
  1325. d<span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 1362790014817</span>
  1326. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>预期为数值的场合,<code>Date</code>实例会自动调用该方法,所以<strong>可以用下面的方法计算时间的间隔</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> start <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1327. <span class="token comment">// ...</span>
  1328. <span class="token keyword">var</span> end <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1329. <span class="token keyword">var</span> elapsed <span class="token operator">=</span> end <span class="token operator">-</span> start<span class="token punctuation">;</span> <span class="token comment">// 自动调用valueOf()方法,返回时间戳的减法运算</span>
  1330. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="_5-2-to-类方法-返回字符串相关"><a href="#_5-2-to-类方法-返回字符串相关" class="header-anchor">#</a> 5.2 to 类方法(返回字符串相关)</h3> <h4 id="_1-date-prototype-tostring-返回一个完整的日期字符串-直接读取实例默认调用此方法"><a href="#_1-date-prototype-tostring-返回一个完整的日期字符串-直接读取实例默认调用此方法" class="header-anchor">#</a> (1)Date.prototype.toString() 返回一个完整的日期字符串(直接读取实例默认调用此方法)</h4> <p><code>toString</code>方法<strong>返回一个完整的日期字符串</strong>。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>var d = new Date(2013, 0, 1);
  1331. d.toString()
  1332. // &quot;Tue Jan 01 2013 00:00:00 GMT+0800 (CST)&quot;
  1333. d
  1334. // &quot;Tue Jan 01 2013 00:00:00 GMT+0800 (CST)&quot;
  1335. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>因为<code>toString</code>是<strong>默认的调用方法</strong>,所以如果<strong>直接读取<code>Date</code>实例,就相当于调用这个方法</strong>。</p> <h4 id="_2-date-prototype-toutcstring-返回对应的utc时间-世界标准时-比北京时间晚8小时"><a href="#_2-date-prototype-toutcstring-返回对应的utc时间-世界标准时-比北京时间晚8小时" class="header-anchor">#</a> (2)Date.prototype.toUTCString() 返回对应的UTC时间(世界标准时),比北京时间晚8小时</h4> <p><code>toUTCString</code>方法返回对应的 UTC 时间,也就是比北京时间晚8个小时。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1336. d<span class="token punctuation">.</span><span class="token function">toUTCString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1337. <span class="token comment">// &quot;Mon, 31 Dec 2012 16:00:00 GMT&quot;</span>
  1338. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h4 id="_3-date-prototype-toisostring-返回对应时间的-iso8601-写法-utc时间"><a href="#_3-date-prototype-toisostring-返回对应时间的-iso8601-写法-utc时间" class="header-anchor">#</a> (3)Date.prototype.toISOString() 返回对应时间的 ISO8601 写法(UTC时间)</h4> <p><code>toISOString</code>方法返回对应时间的 ISO8601 写法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1339. d<span class="token punctuation">.</span><span class="token function">toISOString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1340. <span class="token comment">// &quot;2012-12-31T16:00:00.000Z&quot;</span>
  1341. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>注意,<code>toISOString</code>方法返回的总是 UTC 时区的时间。</strong></p> <h4 id="_4-date-prototype-tojson-返回符合json格式字符串-与toisostring方法的返回结果完全相同。"><a href="#_4-date-prototype-tojson-返回符合json格式字符串-与toisostring方法的返回结果完全相同。" class="header-anchor">#</a> (4)Date.prototype.toJSON() 返回符合JSON格式字符串,与<code>toISOString</code>方法的返回结果完全相同。</h4> <p><code>toJSON</code>方法返回一个符合 JSON 格式的 ISO 日期字符串,与<code>toISOString</code>方法的返回结果完全相同。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1342. d<span class="token punctuation">.</span><span class="token function">toJSON</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1343. <span class="token comment">// &quot;2012-12-31T16:00:00.000Z&quot;</span>
  1344. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h4 id="_5-date-prototype-todatestring-返回日期字符串-不含时分秒"><a href="#_5-date-prototype-todatestring-返回日期字符串-不含时分秒" class="header-anchor">#</a> (5)Date.prototype.toDateString() 返回日期字符串(不含时分秒)</h4> <p><code>toDateString</code>方法<strong>返回日期字符串(不含小时、分和秒)</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1345. d<span class="token punctuation">.</span><span class="token function">toDateString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Tue Jan 01 2013&quot;</span>
  1346. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="_6-date-prototype-totimestring-返回时间字符串-不含年月日"><a href="#_6-date-prototype-totimestring-返回时间字符串-不含年月日" class="header-anchor">#</a> (6)Date.prototype.toTimeString() 返回时间字符串(不含年月日)</h4> <p><code>toTimeString</code>方法<strong>返回时间字符串(不含年月日)</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1347. d<span class="token punctuation">.</span><span class="token function">toTimeString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;00:00:00 GMT+0800 (CST)&quot;</span>
  1348. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h4 id="_7-本地时间"><a href="#_7-本地时间" class="header-anchor">#</a> (7)本地时间</h4> <p>以下三种方法,可以将 Date 实例转为表示本地时间的字符串。</p> <ul><li><code>Date.prototype.toLocaleString()</code>:完整的本地时间。</li> <li><code>Date.prototype.toLocaleDateString()</code>:本地日期(不含小时、分和秒)。</li> <li><code>Date.prototype.toLocaleTimeString()</code>:本地时间(不含年月日)。</li></ul> <p>下面是用法实例。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1349. d<span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1350. <span class="token comment">// 中文版浏览器为&quot;2013年1月1日 上午12:00:00&quot;</span>
  1351. <span class="token comment">// 英文版浏览器为&quot;1/1/2013 12:00:00 AM&quot;</span>
  1352. d<span class="token punctuation">.</span><span class="token function">toLocaleDateString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1353. <span class="token comment">// 中文版浏览器为&quot;2013年1月1日&quot;</span>
  1354. <span class="token comment">// 英文版浏览器为&quot;1/1/2013&quot;</span>
  1355. d<span class="token punctuation">.</span><span class="token function">toLocaleTimeString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  1356. <span class="token comment">// 中文版浏览器为&quot;上午12:00:00&quot;</span>
  1357. <span class="token comment">// 英文版浏览器为&quot;12:00:00 AM&quot;</span>
  1358. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>这三个方法都有两个可选的参数。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>dateObj<span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token punctuation">[</span>locales<span class="token punctuation">[</span><span class="token punctuation">,</span> options<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  1359. dateObj<span class="token punctuation">.</span><span class="token function">toLocaleDateString</span><span class="token punctuation">(</span><span class="token punctuation">[</span>locales<span class="token punctuation">[</span><span class="token punctuation">,</span> options<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  1360. dateObj<span class="token punctuation">.</span><span class="token function">toLocaleTimeString</span><span class="token punctuation">(</span><span class="token punctuation">[</span>locales<span class="token punctuation">[</span><span class="token punctuation">,</span> options<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  1361. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>这两个参数中,<code>locales</code>是一个<strong>指定所用语言的字符串</strong>,<code>options</code>是一个<strong>配置对象</strong>。下面是<code>locales</code>的例子。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1362. d<span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token string">'en-US'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1/1/2013, 12:00:00 AM&quot;</span>
  1363. d<span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token string">'zh-CN'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;2013/1/1 上午12:00:00&quot;</span>
  1364. d<span class="token punctuation">.</span><span class="token function">toLocaleDateString</span><span class="token punctuation">(</span><span class="token string">'en-US'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1/1/2013&quot;</span>
  1365. d<span class="token punctuation">.</span><span class="token function">toLocaleDateString</span><span class="token punctuation">(</span><span class="token string">'zh-CN'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;2013/1/1&quot;</span>
  1366. d<span class="token punctuation">.</span><span class="token function">toLocaleTimeString</span><span class="token punctuation">(</span><span class="token string">'en-US'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;12:00:00 AM&quot;</span>
  1367. d<span class="token punctuation">.</span><span class="token function">toLocaleTimeString</span><span class="token punctuation">(</span><span class="token string">'zh-CN'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;上午12:00:00&quot;</span>
  1368. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>下面是<code>options</code>的例子。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2013</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1369. <span class="token comment">// 时间格式</span>
  1370. <span class="token comment">// 下面的设置是,星期和月份为完整文字,年份和日期为数字</span>
  1371. d<span class="token punctuation">.</span><span class="token function">toLocaleDateString</span><span class="token punctuation">(</span><span class="token string">'en-US'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  1372. <span class="token literal-property property">weekday</span><span class="token operator">:</span> <span class="token string">'long'</span><span class="token punctuation">,</span>
  1373. <span class="token literal-property property">year</span><span class="token operator">:</span> <span class="token string">'numeric'</span><span class="token punctuation">,</span>
  1374. <span class="token literal-property property">month</span><span class="token operator">:</span> <span class="token string">'long'</span><span class="token punctuation">,</span>
  1375. <span class="token literal-property property">day</span><span class="token operator">:</span> <span class="token string">'numeric'</span>
  1376. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1377. <span class="token comment">// &quot;Tuesday, January 1, 2013&quot;</span>
  1378. <span class="token comment">// 指定时区</span>
  1379. d<span class="token punctuation">.</span><span class="token function">toLocaleTimeString</span><span class="token punctuation">(</span><span class="token string">'en-US'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  1380. <span class="token literal-property property">timeZone</span><span class="token operator">:</span> <span class="token string">'UTC'</span><span class="token punctuation">,</span>
  1381. <span class="token literal-property property">timeZoneName</span><span class="token operator">:</span> <span class="token string">'short'</span>
  1382. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1383. <span class="token comment">// &quot;4:00:00 PM UTC&quot;</span>
  1384. d<span class="token punctuation">.</span><span class="token function">toLocaleTimeString</span><span class="token punctuation">(</span><span class="token string">'en-US'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  1385. <span class="token literal-property property">timeZone</span><span class="token operator">:</span> <span class="token string">'Asia/Shanghai'</span><span class="token punctuation">,</span>
  1386. <span class="token literal-property property">timeZoneName</span><span class="token operator">:</span> <span class="token string">'long'</span>
  1387. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1388. <span class="token comment">// &quot;12:00:00 AM China Standard Time&quot;</span>
  1389. <span class="token comment">// 小时周期为12还是24</span>
  1390. d<span class="token punctuation">.</span><span class="token function">toLocaleTimeString</span><span class="token punctuation">(</span><span class="token string">'en-US'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  1391. <span class="token literal-property property">hour12</span><span class="token operator">:</span> <span class="token boolean">false</span>
  1392. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1393. <span class="token comment">// &quot;00:00:00&quot;</span>
  1394. d<span class="token punctuation">.</span><span class="token function">toLocaleTimeString</span><span class="token punctuation">(</span><span class="token string">'en-US'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
  1395. <span class="token literal-property property">hour12</span><span class="token operator">:</span> <span class="token boolean">true</span>
  1396. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1397. <span class="token comment">// &quot;12:00:00 AM&quot;</span>
  1398. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h3 id="_5-3-get类-获取年、月、日、时、分、秒等"><a href="#_5-3-get类-获取年、月、日、时、分、秒等" class="header-anchor">#</a> 5.3 get类 (获取年、月、日、时、分、秒等)</h3> <p><code>Date</code>对象提供了一系列<code>get*</code>方法,用来获取实例对象某个方面的值。</p> <ul><li><code>getTime()</code>:返回实例距离1970年1月1日00:00:00的毫秒数,等同于<code>valueOf</code>方法。<strong>(时间戳)</strong></li> <li><code>getDate()</code>:返回实例对象对应每个月的几号(从1开始)。<strong>(日)</strong></li> <li><code>getDay()</code>:返回星期几,星期日为0,星期一为1,以此类推。<strong>(星期)</strong></li> <li><code>getFullYear()</code>:返回四位的年份。<strong>(年)</strong></li> <li><code>getMonth()</code>:返回月份(0表示1月,11表示12月)。<strong>(月)</strong></li> <li><code>getHours()</code>:返回小时(0-23)。<strong>(时)</strong></li> <li><code>getMilliseconds()</code>:返回毫秒(0-999)。<strong>(毫秒)</strong></li> <li><code>getMinutes()</code>:返回分钟(0-59)。<strong>(分)</strong></li> <li><code>getSeconds()</code>:返回秒(0-59)。<strong>(秒)</strong></li> <li><code>getTimezoneOffset()</code>:返回当前时间与 UTC 的时区差异,以分钟表示,返回结果考虑到了夏令时因素。<strong>(与UTC差异)</strong></li></ul> <p>所有这些<code>get*</code>方法返回的都是<strong>整数</strong>,不同方法返回值的范围不一样。</p> <ul><li>分钟和秒:0 到 59</li> <li>小时:0 到 23</li> <li>星期:0(星期天)到 6(星期六)</li> <li>日期:1 到 31</li> <li>月份:0(一月)到 11(十二月)</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'January 6, 2013'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1399. d<span class="token punctuation">.</span><span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 6</span>
  1400. d<span class="token punctuation">.</span><span class="token function">getMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 0</span>
  1401. d<span class="token punctuation">.</span><span class="token function">getFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 2013</span>
  1402. d<span class="token punctuation">.</span><span class="token function">getTimezoneOffset</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// -480 UTC时间减去当前时间,单位是分钟</span>
  1403. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,最后一行返回<code>-480</code>,即 UTC 时间减去当前时间,单位是分钟。<code>-480</code>表示 UTC 比当前时间少480分钟,即当前时区比 UTC 早8个小时。</p> <h6 id="例子-计算本年度还剩下多少天"><a href="#例子-计算本年度还剩下多少天" class="header-anchor">#</a> 例子:计算本年度还剩下多少天</h6> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">leftDays</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1404. <span class="token keyword">var</span> today <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1405. <span class="token keyword">var</span> endYear <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>today<span class="token punctuation">.</span><span class="token function">getFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">31</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">,</span> <span class="token number">59</span><span class="token punctuation">,</span> <span class="token number">59</span><span class="token punctuation">,</span> <span class="token number">999</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1406. <span class="token keyword">var</span> msPerDay <span class="token operator">=</span> <span class="token number">24</span> <span class="token operator">*</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">1000</span><span class="token punctuation">;</span> <span class="token comment">// 一天的毫秒数</span>
  1407. <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token punctuation">(</span>endYear<span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> today<span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">/</span> msPerDay<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1408. <span class="token punctuation">}</span>
  1409. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面这些<code>get*</code>方法返回的都是当前时区的时间,<code>Date</code>对象还提供了这些方法对应的 UTC 版本,用来返回 UTC 时间。</p> <p><strong>获取UTC时区相关方法:</strong></p> <ul><li><code>getUTCDate()</code></li> <li><code>getUTCFullYear()</code></li> <li><code>getUTCMonth()</code></li> <li><code>getUTCDay()</code></li> <li><code>getUTCHours()</code></li> <li><code>getUTCMinutes()</code></li> <li><code>getUTCSeconds()</code></li> <li><code>getUTCMilliseconds()</code></li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'January 6, 2013'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1410. d<span class="token punctuation">.</span><span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 6</span>
  1411. d<span class="token punctuation">.</span><span class="token function">getUTCDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 5</span>
  1412. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,实例对象<code>d</code>表示当前时区(东八时区)的1月6日0点0分0秒,这个时间对于当前时区来说是1月6日,所以<code>getDate</code>方法返回6,对于 UTC 时区来说是1月5日,所以<code>getUTCDate</code>方法返回5。</p> <h3 id="_5-4-set-类方法"><a href="#_5-4-set-类方法" class="header-anchor">#</a> 5.4 set 类方法</h3> <p><code>Date</code>对象提供了一系列<code>set*</code>方法,用来设置实例对象的各个方面。</p> <ul><li><code>setDate(date)</code>:设置实例对象对应的每个月的几<strong>号</strong>(1-31),返回改变后毫秒时间戳。</li> <li><code>setFullYear(year [, month, date])</code>:设置四位<strong>年份</strong>。</li> <li><code>setHours(hour [, min, sec, ms])</code>:设置<strong>小时</strong>(0-23)。</li> <li><code>setMilliseconds()</code>:设置<strong>毫秒</strong>(0-999)。</li> <li><code>setMinutes(min [, sec, ms])</code>:设置<strong>分钟</strong>(0-59)。</li> <li><code>setMonth(month [, date])</code>:设置<strong>月份</strong>(0-11)。</li> <li><code>setSeconds(sec [, ms])</code>:设置<strong>秒</strong>(0-59)。</li> <li><code>setTime(milliseconds)</code>:设置<strong>毫秒时间戳</strong>。</li></ul> <p>这些方法基本是跟<code>get*</code>方法一一对应的,但是没有<code>setDay</code>方法,因为星期几是计算出来的,而不是设置的。另外,需要注意的是,凡是涉及到设置月份,都是从0开始算的,即<code>0</code>是1月,<code>11</code>是12月。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span> <span class="token punctuation">(</span><span class="token string">'January 6, 2013'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1413. d <span class="token comment">// Sun Jan 06 2013 00:00:00 GMT+0800 (CST)</span>
  1414. d<span class="token punctuation">.</span><span class="token function">setDate</span><span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">)</span> <span class="token comment">// 1357660800000</span>
  1415. d <span class="token comment">// Wed Jan 09 2013 00:00:00 GMT+0800 (CST)</span>
  1416. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><code>set*</code>方法的参数都会自动折算。以<code>setDate</code>为例,如果参数超过当月的最大天数,则向下一个月顺延,如果参数是负数,表示从上个月的最后一天开始减去的天数。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'January 6, 2013'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1417. d1<span class="token punctuation">.</span><span class="token function">setDate</span><span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">)</span> <span class="token comment">// 1359648000000</span>
  1418. d1 <span class="token comment">// Fri Feb 01 2013 00:00:00 GMT+0800 (CST)</span>
  1419. <span class="token keyword">var</span> d2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span> <span class="token punctuation">(</span><span class="token string">'January 6, 2013'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1420. d<span class="token punctuation">.</span><span class="token function">setDate</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// 1356796800000</span>
  1421. d <span class="token comment">// Sun Dec 30 2012 00:00:00 GMT+0800 (CST)</span>
  1422. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><code>**set</code>类方法和<code>get</code>类方法,可以结合使用,得到相对时间。**</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1423. <span class="token comment">// 将日期向后推1000天</span>
  1424. d<span class="token punctuation">.</span><span class="token function">setDate</span><span class="token punctuation">(</span>d<span class="token punctuation">.</span><span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1425. <span class="token comment">// 将时间设为6小时后</span>
  1426. d<span class="token punctuation">.</span><span class="token function">setHours</span><span class="token punctuation">(</span>d<span class="token punctuation">.</span><span class="token function">getHours</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1427. <span class="token comment">// 将年份设为去年</span>
  1428. d<span class="token punctuation">.</span><span class="token function">setFullYear</span><span class="token punctuation">(</span>d<span class="token punctuation">.</span><span class="token function">getFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1429. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><code>set*</code>系列方法除了<code>setTime()</code>,都有对应的 UTC 版本,即<strong>设置 UTC 时区的时间</strong>。</p> <ul><li><code>setUTCDate()</code></li> <li><code>setUTCFullYear()</code></li> <li><code>setUTCHours()</code></li> <li><code>setUTCMilliseconds()</code></li> <li><code>setUTCMinutes()</code></li> <li><code>setUTCMonth()</code></li> <li><code>setUTCSeconds()</code></li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'January 6, 2013'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1430. d<span class="token punctuation">.</span><span class="token function">getUTCHours</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 16</span>
  1431. d<span class="token punctuation">.</span><span class="token function">setUTCHours</span><span class="token punctuation">(</span><span class="token number">22</span><span class="token punctuation">)</span> <span class="token comment">// 1357423200000</span>
  1432. d <span class="token comment">// Sun Jan 06 2013 06:00:00 GMT+0800 (CST)</span>
  1433. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,本地时区(东八时区)的1月6日0点0分,是 UTC 时区的前一天下午16点。设为 UTC 时区的22点以后,就变为本地时区的上午6点。</p> <h2 id="十、regexp-对象"><a href="#十、regexp-对象" class="header-anchor">#</a> 十、RegExp 对象</h2> <p><code>RegExp</code>对象提供正则表达式的功能。</p> <h2 id="_1、概述-6"><a href="#_1、概述-6" class="header-anchor">#</a> 1、概述</h2> <p><strong>正则表达式(regular expression)是一种表达文本模式(即字符串结构)的方法,有点像字符串的模板,常常用来按照“给定模式”匹配文本</strong>。比如,正则表达式给出一个 Email 地址的模式,然后用它来确定一个字符串是否为 Email 地址。JavaScript 的正则表达式体系是参照 Perl 5 建立的。</p> <p><strong>新建正则表达式有两种方法:</strong></p> <p><strong>一种是使用字面量,以斜杠表示开始和结束。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xyz</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span> <span class="token comment">// 效率较高,且直观,推荐。</span>
  1434. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>另一种是使用<code>RegExp</code>构造函数。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">'xyz'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1435. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面两种写法是等价的,都新建了一个内容为<code>xyz</code>的正则表达式对象。它们的主要区别是,第一种方法在引擎编译代码时,就会新建正则表达式,第二种方法在运行时新建正则表达式,所以前者的效率较高。而且,前者比较便利和直观,所以实际应用中,基本上都采用字面量定义正则表达式。</p> <p><code>RegExp</code>构造函数还可以接受第二个参数,表示修饰符(详细解释见下文)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">'xyz'</span><span class="token punctuation">,</span> <span class="token string">'i'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// i为修饰符</span>
  1436. <span class="token comment">// 等价于</span>
  1437. <span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">xyz</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">;</span>
  1438. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,正则表达式<code>/xyz/</code>有一个修饰符<code>i</code>。</p> <h2 id="_2、实例属性"><a href="#_2、实例属性" class="header-anchor">#</a> 2、实例属性</h2> <p>正则对象的实例属性分成两类。</p> <p>一类是<strong>修饰符相关</strong>,用于了解设置了什么修饰符。</p> <ul><li><code>RegExp.prototype.ignoreCase</code>:返回一个布尔值,表示是否设置了<code>i</code>修饰符。</li> <li><code>RegExp.prototype.global</code>:返回一个布尔值,表示是否设置了<code>g</code>修饰符。</li> <li><code>RegExp.prototype.multiline</code>:返回一个布尔值,表示是否设置了<code>m</code>修饰符。</li> <li><code>RegExp.prototype.flags</code>:返回一个字符串,包含了已经设置的所有修饰符,按字母排序。</li></ul> <p>上面四个属性都是<strong>只读</strong>的。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">abc</span><span class="token regex-delimiter">/</span><span class="token regex-flags">igm</span></span><span class="token punctuation">;</span>
  1439. r<span class="token punctuation">.</span>ignoreCase <span class="token comment">// true 是否设置i修饰符</span>
  1440. r<span class="token punctuation">.</span>global <span class="token comment">// true 是否设置g修饰符</span>
  1441. r<span class="token punctuation">.</span>multiline <span class="token comment">// true 是否设置m修饰符</span>
  1442. r<span class="token punctuation">.</span>flags <span class="token comment">// 'gim' 字符串,包含设置的所有修饰符,按字母排序</span>
  1443. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>另一类是与<strong>修饰符无关的属性</strong>,主要是下面两个。</p> <ul><li><code>RegExp.prototype.lastIndex</code>:返回一个整数,表示下一次开始搜索的位置。该属性可读写,但是只在进行连续搜索时有意义,详细介绍请看后文。</li> <li><code>RegExp.prototype.source</code>:返回正则表达式的字符串形式(不包括反斜杠),该属性只读。</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">abc</span><span class="token regex-delimiter">/</span><span class="token regex-flags">igm</span></span><span class="token punctuation">;</span>
  1444. r<span class="token punctuation">.</span>lastIndex <span class="token comment">// 0</span>
  1445. r<span class="token punctuation">.</span>source <span class="token comment">// &quot;abc&quot; 正则表达式的字符串形式</span>
  1446. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="_3、实例方法-3"><a href="#_3、实例方法-3" class="header-anchor">#</a> 3、实例方法</h2> <h3 id="_3-1-regexp-prototype-test-当前模式是否匹配参数字符串-返回布尔值"><a href="#_3-1-regexp-prototype-test-当前模式是否匹配参数字符串-返回布尔值" class="header-anchor">#</a> 3.1 RegExp.prototype.test() 当前模式是否匹配参数字符串,返回布尔值</h3> <p>正则实例对象的<code>test</code>方法<strong>返回一个布尔值,表示当前模式是否能匹配参数字符串</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">cat</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'cats and dogs'</span><span class="token punctuation">)</span> <span class="token comment">// true 验证字符串是否包含cat</span>
  1447. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码验证参数字符串之中是否包含<code>cat</code>,结果返回<code>true</code>。</p> <p>如果正则表达式带有<code>g</code>修饰符,则每一次<code>test</code>方法都从上一次结束的位置开始向后匹配。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">x</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span> <span class="token comment">// 带有g全局匹配修饰符</span>
  1448. <span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'_x_x'</span><span class="token punctuation">;</span>
  1449. r<span class="token punctuation">.</span>lastIndex <span class="token comment">// 0 下一次开始搜索的位置</span>
  1450. r<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// true 内部原理:从0位置开始匹配,匹配到'x'在1位置,返回true,并把lastIndex属性设置为2,</span>
  1451. r<span class="token punctuation">.</span>lastIndex <span class="token comment">// 2 下一次开始搜索的位置</span>
  1452. r<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// true 内部原理:从2位置开始匹配,匹配到'x'在3位置,返回true,并把lastIndex属性设置为4</span>
  1453. r<span class="token punctuation">.</span>lastIndex <span class="token comment">// 4 下一次开始搜索的位置</span>
  1454. r<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// false 内部原理:从4位置开始匹配,匹配不到’x‘,返回false</span>
  1455. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码的正则表达式使用了<code>g</code>修饰符,表示是全局搜索,会有多个结果。接着,三次使用<code>test</code>方法,每一次开始搜索的位置都是上一次匹配的后一个位置。</p> <p>带有<code>g</code>修饰符时,可以通过正则对象的<code>lastIndex</code>属性指定开始搜索的位置。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">x</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
  1456. <span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'_x_x'</span><span class="token punctuation">;</span>
  1457. r<span class="token punctuation">.</span>lastIndex <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">;</span> <span class="token comment">// lastIndex属性可读写,改写后,test方法将从该位置开始匹配</span>
  1458. r<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// false</span>
  1459. r<span class="token punctuation">.</span>lastIndex <span class="token comment">// 0 重置为0</span>
  1460. r<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// true</span>
  1461. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码指定从字符串的第五个位置开始搜索,这个位置为空,所以返回<code>false</code>。同时,<code>lastIndex</code>属性重置为<code>0</code>,所以第二次执行<code>r.test(s)</code>会返回<code>true</code>。</p> <p><strong>注意</strong>,<strong>带有<code>g</code>修饰符时,正则表达式内部会记住上一次的<code>lastIndex</code>属性</strong>,这时不应该更换所要匹配的字符串,否则会有一些难以察觉的错误。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">bb</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
  1462. r<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'bb'</span><span class="token punctuation">)</span> <span class="token comment">// true 由于带有g修饰符,匹配后会把lastIndex属性修改为了2</span>
  1463. r<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'-bb-'</span><span class="token punctuation">)</span> <span class="token comment">// false 从2位置开始匹配'bb',并没有匹配到</span>
  1464. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,由于正则表达式<code>r</code>是从上一次的<code>lastIndex</code>位置开始匹配,导致第二次执行<code>test</code>方法时出现预期以外的结果。</p> <p><code>lastIndex</code>属性只对同一个正则表达式有效,所以下面这样写是错误的。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
  1465. <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'babaa'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> count<span class="token operator">++</span><span class="token punctuation">;</span>
  1466. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码会导致无限循环,因为<code>while</code>循环的每次匹配条件都是一个新的正则表达式,导致<code>lastIndex</code>属性总是等于0。</p> <p>如果正则模式是一个<strong>空字符串,则匹配所有字符串</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span>
  1467. <span class="token comment">// true</span>
  1468. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_3-2-regexp-prototype-exec-返回匹配结果-如匹配则返回一个数组-成员是匹配到的子字符串-否则返回null"><a href="#_3-2-regexp-prototype-exec-返回匹配结果-如匹配则返回一个数组-成员是匹配到的子字符串-否则返回null" class="header-anchor">#</a> 3.2 RegExp.prototype.exec() 返回匹配结果,如匹配则返回一个数组,成员是匹配到的子字符串,否则返回null</h3> <p>正则实例对象的<code>exec</code>方法,用来<strong>返回匹配结果。如果发现匹配,就返回一个数组,成员是匹配成功的子字符串,否则返回<code>null</code>。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'_x_x'</span><span class="token punctuation">;</span>
  1469. <span class="token keyword">var</span> r1 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">x</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
  1470. <span class="token keyword">var</span> r2 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">y</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
  1471. r1<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// [&quot;x&quot;] 返回一个成员</span>
  1472. r2<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// null</span>
  1473. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,正则对象<code>r1</code>匹配成功,返回一个数组,成员是匹配结果;正则对象<code>r2</code>匹配失败,返回<code>null</code>。</p> <p><strong>如果正则表示式包含圆括号(即含有“组匹配”),则返回的数组会包括多个成员</strong>。第一个成员是整个匹配成功的结果,后面的成员就是圆括号对应的匹配成功的组。也就是说,第二个成员对应第一个括号,第三个成员对应第二个括号,以此类推。整个数组的<code>length</code>属性等于组匹配的数量再加1。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'_x_x'</span><span class="token punctuation">;</span>
  1474. <span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">_(x)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span> <span class="token comment">// 含有圆括号,组匹配</span>
  1475. r<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// [&quot;_x&quot;, &quot;x&quot;] 返回多个成员,第一个成员是整个匹配的结果,第二个成员是圆括号匹配的结果</span>
  1476. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码的<code>exec</code>方法,返回一个数组。第一个成员是整个匹配的结果,第二个成员是圆括号匹配的结果。</p> <p><code>exec</code>方法的返回数组还包含以下两个属性:</p> <ul><li><code>input</code>:整个原字符串。</li> <li><code>index</code>:整个模式匹配成功的开始位置(从0开始计数)。</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a(b+)a</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
  1477. <span class="token keyword">var</span> arr <span class="token operator">=</span> r<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'_abbba_aba_'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1478. arr <span class="token comment">// [&quot;abbba&quot;, &quot;bbb&quot;] // 第一个成员是整个匹配的结果,第二个成员是圆括号匹配的结果</span>
  1479. arr<span class="token punctuation">.</span>index <span class="token comment">// 1 表示是在1位置开始匹配成功的</span>
  1480. arr<span class="token punctuation">.</span>input <span class="token comment">// &quot;_abbba_aba_&quot;</span>
  1481. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中的<code>index</code>属性等于1,是因为从原字符串的第二个位置开始匹配成功。</p> <p>如果正则表达式加上<code>g</code>修饰符,则可以使用多次<code>exec</code>方法,下一次搜索的位置从上一次匹配成功结束的位置开始。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span> <span class="token comment">// 带'g'修饰符</span>
  1482. <span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'abc_abc_abc'</span>
  1483. <span class="token keyword">var</span> r1 <span class="token operator">=</span> reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1484. r1 <span class="token comment">// [&quot;a&quot;]</span>
  1485. r1<span class="token punctuation">.</span>index <span class="token comment">// 0 在0位置匹配成功</span>
  1486. reg<span class="token punctuation">.</span>lastIndex <span class="token comment">// 1 下一次匹配开始位置为1</span>
  1487. <span class="token keyword">var</span> r2 <span class="token operator">=</span> reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1488. r2 <span class="token comment">// [&quot;a&quot;]</span>
  1489. r2<span class="token punctuation">.</span>index <span class="token comment">// 4 在4位置匹配成功</span>
  1490. reg<span class="token punctuation">.</span>lastIndex <span class="token comment">// 5 下一次匹配开始位置为5</span>
  1491. <span class="token keyword">var</span> r3 <span class="token operator">=</span> reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1492. r3 <span class="token comment">// [&quot;a&quot;]</span>
  1493. r3<span class="token punctuation">.</span>index <span class="token comment">// 8 在8位置匹配成功</span>
  1494. reg<span class="token punctuation">.</span>lastIndex <span class="token comment">// 9 下一次匹配开始位置为9</span>
  1495. <span class="token keyword">var</span> r4 <span class="token operator">=</span> reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1496. r4 <span class="token comment">// null 没有匹配到返回null</span>
  1497. reg<span class="token punctuation">.</span>lastIndex <span class="token comment">// 0 下一次匹配位置为0</span>
  1498. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>上面代码连续用了四次<code>exec</code>方法,前三次都是从上一次匹配结束的位置向后匹配。当第三次匹配结束以后,整个字符串已经到达尾部,匹配结果返回<code>null</code>,正则实例对象的<code>lastIndex</code>属性也重置为<code>0</code>,意味着第四次匹配将从头开始。</p> <p>利用<code>g</code>修饰符允许多次匹配的特点,可以用一个循环完成全部匹配。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
  1499. <span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'abc_abc_abc'</span>
  1500. <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1501. <span class="token keyword">var</span> match <span class="token operator">=</span> reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1502. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>match<span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
  1503. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'#'</span> <span class="token operator">+</span> match<span class="token punctuation">.</span>index <span class="token operator">+</span> <span class="token string">':'</span> <span class="token operator">+</span> match<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1504. <span class="token punctuation">}</span>
  1505. <span class="token comment">// #0:a</span>
  1506. <span class="token comment">// #4:a</span>
  1507. <span class="token comment">// #8:a</span>
  1508. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,只要<code>exec</code>方法不返回<code>null</code>,就会一直循环下去,每次输出匹配的位置和匹配的文本。</p> <p>正则实例对象的<code>lastIndex</code>属性不仅可读,还可写。设置了<code>g</code>修饰符的时候,只要手动设置了<code>lastIndex</code>的值,就会从指定位置开始匹配。</p> <h2 id="_4、字符串的实例方法"><a href="#_4、字符串的实例方法" class="header-anchor">#</a> 4、字符串的实例方法</h2> <p><strong>字符串的实例方法之中,有4种与正则表达式有关。</strong></p> <ul><li><code>String.prototype.match()</code>:返回一个数组,成员是所有匹配的子字符串。</li> <li><code>String.prototype.search()</code>:按照给定的正则表达式进行搜索,返回一个整数,表示匹配开始的位置。</li> <li><code>String.prototype.replace()</code>:按照给定的正则表达式进行替换,返回替换后的字符串。</li> <li><code>String.prototype.split()</code>:按照给定规则进行字符串分割,返回一个数组,包含分割后的各个成员。</li></ul> <h3 id="_4-1-string-prototype-match-匹配-返回匹配结果数组或null"><a href="#_4-1-string-prototype-match-匹配-返回匹配结果数组或null" class="header-anchor">#</a> 4.1 String.prototype.match() 匹配,返回匹配结果数组或null</h3> <p>字符串实例对象的<code>match</code>方法<strong>对字符串进行正则匹配,返回匹配结果</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'_x_x'</span><span class="token punctuation">;</span>
  1509. <span class="token keyword">var</span> r1 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">x</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
  1510. <span class="token keyword">var</span> r2 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">y</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
  1511. s<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span>r1<span class="token punctuation">)</span> <span class="token comment">// [&quot;x&quot;] // 未带g修饰符,匹配到一个即返回结果</span>
  1512. s<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span>r2<span class="token punctuation">)</span> <span class="token comment">// null</span>
  1513. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>从上面代码可以看到,字符串的<code>match</code>方法与正则对象的<code>exec</code>方法非常类似:匹配成功返回一个数组,匹配失败返回<code>null</code>。</p> <p>如果正则表达式带有<code>g</code>修饰符,则该方法与正则对象的<code>exec</code>方法行为不同,会一次性返回所有匹配成功的结果。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'abba'</span><span class="token punctuation">;</span>
  1514. <span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
  1515. s<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;, &quot;a&quot;] 带g修饰符,会返回全部结果到数组成员</span>
  1516. r<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;] 正则实例的exec方法只返回一个</span>
  1517. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>设置<strong>正则表达式的<code>lastIndex</code>属性,对<code>match</code>方法无效</strong>,匹配总是从字符串的第一个字符开始。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> r <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a|b</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
  1518. r<span class="token punctuation">.</span>lastIndex <span class="token operator">=</span> <span class="token number">7</span><span class="token punctuation">;</span>
  1519. <span class="token string">'xaxb'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span> <span class="token comment">// ['a', 'b']</span>
  1520. r<span class="token punctuation">.</span>lastIndex <span class="token comment">// 0</span>
  1521. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码表示,设置正则对象的<code>lastIndex</code>属性是无效的。</p> <h3 id="_4-2-string-prototype-search-返回第一个满足匹配条件的位置-不满足则返-1"><a href="#_4-2-string-prototype-search-返回第一个满足匹配条件的位置-不满足则返-1" class="header-anchor">#</a> 4.2 String.prototype.search() 返回第一个满足匹配条件的位置,不满足则返-1</h3> <p>字符串对象的<code>search</code>方法,<strong>返回第一个满足条件的匹配结果在整个字符串中的位置</strong>。<strong>如果没有任何匹配,则返回<code>-1</code>。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'_x_x'</span><span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">x</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span>
  1522. <span class="token comment">// 1</span>
  1523. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,第一个匹配结果出现在字符串的<code>1</code>号位置。</p> <h3 id="_4-3-string-prototype-replace-替换匹配的值-参数一是子串或正则-参数二是替换内容"><a href="#_4-3-string-prototype-replace-替换匹配的值-参数一是子串或正则-参数二是替换内容" class="header-anchor">#</a> 4.3 String.prototype.replace() 替换匹配的值,参数一是子串或正则,参数二是替换内容</h3> <p>字符串对象的<code>replace</code>方法可以<strong>替换匹配的值</strong>。它接受两个参数,第一个是正则表达式,表示搜索模式,第二个是替换的内容。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>search<span class="token punctuation">,</span> replacement<span class="token punctuation">)</span>
  1524. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>正则表达式如果不加<code>g</code>修饰符,就替换第一个匹配成功的值,否则替换所有匹配成功的值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'aaa'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;baa&quot;</span>
  1525. <span class="token string">'aaa'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;baa&quot; 不带g修饰符,只匹配并替换第一个</span>
  1526. <span class="token string">'aaa'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;bbb&quot; 带g修饰符,匹配全局,并替换全部匹配到的值</span>
  1527. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,最后一个正则表达式使用了<code>g</code>修饰符,导致所有的<code>b</code>都被替换掉了。</p> <p><code>replace</code>方法的一个应用,就是消除字符串首尾两端的空格。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">' #id div.class '</span><span class="token punctuation">;</span>
  1528. str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\s+|\s+$</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
  1529. <span class="token comment">// &quot;#id div.class&quot;</span>
  1530. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>replace</code>方法的<strong>第二个参数可以使用美元符号<code>$</code>,用来指代所替换的内容</strong>。</p> <ul><li><code>$&amp;</code>:匹配的子字符串。</li> <li>$`:匹配结果前面的文本。</li> <li><code>$'</code>:匹配结果后面的文本。</li> <li><code>$n</code>:匹配成功的第<code>n</code>组内容,<code>n</code>是从1开始的自然数。</li> <li><code>$$</code>:指代美元符号<code>$</code>。</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'hello world'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(\w+)\s(\w+)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">,</span> <span class="token string">'$2 $1'</span><span class="token punctuation">)</span>
  1531. <span class="token comment">// &quot;world hello&quot;</span>
  1532. <span class="token comment">/*说明:
  1533. $n 用于组内容,$2为匹配到的第二组,即world; $1为匹配到的第一组,即hello
  1534. */</span>
  1535. <span class="token string">'abc'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'[$`-$&amp;-$\'-$$]'</span><span class="token punctuation">)</span>
  1536. <span class="token comment">// &quot;a[a-b-c-$]c&quot;</span>
  1537. <span class="token comment">/*说明:
  1538. $`为匹配结果 b 前面的文本,即 a
  1539. $&amp;为匹配的子字符串,即 b
  1540. $’为匹配结果 b 后面的文本,即 c
  1541. $$代表$符号本身
  1542. */</span>
  1543. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>上面代码中,第一个例子是将匹配的组互换位置,第二个例子是改写匹配的值。</p> <p><code>replace</code>方法的第<strong>二个参数还可以是一个函数,将每一个匹配内容替换为函数返回值</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'3 and 5'</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[0-9]+</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">match</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1544. <span class="token keyword">return</span> <span class="token number">2</span> <span class="token operator">*</span> match<span class="token punctuation">;</span> <span class="token comment">// match参数是匹配到的内容</span>
  1545. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1546. <span class="token comment">// &quot;6 and 10&quot;</span>
  1547. <span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token string">'The quick brown fox jumped over the lazy dog.'</span><span class="token punctuation">;</span>
  1548. <span class="token keyword">var</span> pattern <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">quick|brown|lazy</span><span class="token regex-delimiter">/</span><span class="token regex-flags">ig</span></span><span class="token punctuation">;</span>
  1549. a<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token function">replacer</span><span class="token punctuation">(</span><span class="token parameter">match</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1550. <span class="token keyword">return</span> match<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1551. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1552. <span class="token comment">// The QUICK BROWN fox jumped over the LAZY dog.</span>
  1553. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>作为<code>replace</code>方法第二个参数的替换函数,可以接受多个参数。其中,第一个参数是捕捉到的内容,第二个参数是捕捉到的组匹配(<strong>有多少个组匹配,就有多少个对应的参数</strong>)。此外,最后还可以添加两个参数,倒数第二个参数是捕捉到的内容在整个字符串中的位置(比如从第五个位置开始),最后一个参数是原字符串。下面是一个网页模板替换的例子。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> prices <span class="token operator">=</span> <span class="token punctuation">{</span>
  1554. <span class="token string-property property">'p1'</span><span class="token operator">:</span> <span class="token string">'$1.99'</span><span class="token punctuation">,</span>
  1555. <span class="token string-property property">'p2'</span><span class="token operator">:</span> <span class="token string">'$9.99'</span><span class="token punctuation">,</span>
  1556. <span class="token string-property property">'p3'</span><span class="token operator">:</span> <span class="token string">'$5.00'</span>
  1557. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  1558. <span class="token keyword">var</span> template <span class="token operator">=</span> <span class="token string">'&lt;span id=&quot;p1&quot;&gt;&lt;/span&gt;'</span>
  1559. <span class="token operator">+</span> <span class="token string">'&lt;span id=&quot;p2&quot;&gt;&lt;/span&gt;'</span>
  1560. <span class="token operator">+</span> <span class="token string">'&lt;span id=&quot;p3&quot;&gt;&lt;/span&gt;'</span><span class="token punctuation">;</span>
  1561. template<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>
  1562. <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(&lt;span id=&quot;)(.*?)(&quot;&gt;)(&lt;\/span&gt;)</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span>
  1563. <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">match<span class="token punctuation">,</span> $1<span class="token punctuation">,</span> $2<span class="token punctuation">,</span> $3<span class="token punctuation">,</span> $4<span class="token punctuation">,</span> index<span class="token punctuation">,</span> str</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  1564. <span class="token keyword">return</span> $1 <span class="token operator">+</span> $2 <span class="token operator">+</span> $3 <span class="token operator">+</span> prices<span class="token punctuation">[</span>$2<span class="token punctuation">]</span> <span class="token operator">+</span> $4<span class="token punctuation">;</span>
  1565. <span class="token punctuation">}</span>
  1566. <span class="token punctuation">)</span><span class="token punctuation">;</span>
  1567. <span class="token comment">// &quot;&lt;span id=&quot;p1&quot;&gt;$1.99&lt;/span&gt;&lt;span id=&quot;p2&quot;&gt;$9.99&lt;/span&gt;&lt;span id=&quot;p3&quot;&gt;$5.00&lt;/span&gt;&quot;</span>
  1568. <span class="token comment">// 说明:</span>
  1569. <span class="token comment">// match 整个匹配到的内容:&lt;span id=&quot;p*&quot;&gt;&lt;/span&gt;</span>
  1570. <span class="token comment">// 以下参数分别是匹配到的组内容,有多少组就对应多少个参数</span>
  1571. <span class="token comment">// $1 &lt;span id=&quot;</span>
  1572. <span class="token comment">// $2 p1~p3</span>
  1573. <span class="token comment">// $3 &quot;&gt;</span>
  1574. <span class="token comment">// $4 &lt;/span&gt;</span>
  1575. <span class="token comment">// 后面还可以接受两个参数:</span>
  1576. <span class="token comment">// 倒数第二个是:捕捉到的内容(即match的内容)在整个字符串中的位置</span>
  1577. <span class="token comment">// 最后一个是:原字符串</span>
  1578. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><p>上面代码的捕捉模式中,有四个括号,所以会产生四个组匹配,在匹配函数中用<code>$1</code>到<code>$4</code>表示。匹配函数的作用是将价格插入模板中。</p> <h3 id="_4-4-string-prototype-split-按给定规则分割字符串-返回数组"><a href="#_4-4-string-prototype-split-按给定规则分割字符串-返回数组" class="header-anchor">#</a> 4.4 String.prototype.split() 按给定规则分割字符串,返回数组</h3> <p>字符串对象的<code>split</code>方法<strong>按照正则规则分割字符串,返回一个由分割后的各个部分组成的数组。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>str<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span>separator<span class="token punctuation">,</span> <span class="token punctuation">[</span>limit<span class="token punctuation">]</span><span class="token punctuation">)</span>
  1579. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>该方法接受<strong>两个参数</strong>,第一个参数是正则表达式,表示分隔规则,第二个参数是返回数组的最大成员数(数组长度)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 非正则分隔</span>
  1580. <span class="token string">'a, b,c, d'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">)</span>
  1581. <span class="token comment">// [ 'a', ' b', 'c', ' d' ]</span>
  1582. <span class="token comment">// 正则分隔,去除多余的空格</span>
  1583. <span class="token string">'a, b,c, d'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">, *</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span>
  1584. <span class="token comment">// [ 'a', 'b', 'c', 'd' ]</span>
  1585. <span class="token comment">// 指定返回数组的最大成员(数组长度)</span>
  1586. <span class="token string">'a, b,c, d'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">, *</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>
  1587. <span class="token punctuation">[</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span> <span class="token punctuation">]</span>
  1588. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码使用正则表达式,去除了子字符串的逗号后面的空格。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 例一</span>
  1589. <span class="token string">'aaa*a*'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a*</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span>
  1590. <span class="token comment">// [ '', '*', '*' ] 分隔符,即被过滤的符号,</span>
  1591. <span class="token comment">// 例二</span>
  1592. <span class="token string">'aaa**a*'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a*</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span>
  1593. <span class="token comment">// [&quot;&quot;, &quot;*&quot;, &quot;*&quot;, &quot;*&quot;]</span>
  1594. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码的分割规则是0次或多次的<code>a</code>,由于正则默认是贪婪匹配,所以例一的第一个分隔符是<code>aaa</code>,第二个分割符是<code>a</code>,将字符串分成三个部分,包含开始处的空字符串。例二的第一个分隔符是<code>aaa</code>,第二个分隔符是0个<code>a</code>(即空字符),第三个分隔符是<code>a</code>,所以将字符串分成四个部分。</p> <p>如果正则表达式<strong>带有括号</strong>,则括号匹配的部分也会作为数组成员返回。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'aaa*a*'</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(a*)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span>
  1595. <span class="token comment">// [ '', 'aaa', '*', 'a', '*' ]</span>
  1596. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码的正则表达式使用了括号,第一个组匹配是<code>aaa</code>,第二个组匹配是<code>a</code>,它们都作为数组成员返回。</p> <h2 id="_5、匹配规则"><a href="#_5、匹配规则" class="header-anchor">#</a> 5、匹配规则</h2> <p>搭配菜鸟教程一起学习更容易理解:<a href="https://www.runoob.com/regexp/regexp-tutorial.html" target="_blank" rel="noopener noreferrer">https://www.runoob.com/regexp/regexp-tutorial.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>正则表达式在线测试与常用正则表达式:<a href="http://c.runoob.com/front-end/854" target="_blank" rel="noopener noreferrer">http://c.runoob.com/front-end/854<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>正则表达式的规则很复杂,下面一一介绍这些规则。</p> <h3 id="_5-1-字面量字符和元字符"><a href="#_5-1-字面量字符和元字符" class="header-anchor">#</a> 5.1 字面量字符和元字符</h3> <p><strong>大部分字符在正则表达式中,就是字面的含义,比如<code>/a/</code>匹配<code>a</code>,<code>/b/</code>匹配<code>b</code></strong>。如果在正则表达式之中,某个字符只表示它字面的含义(就像前面的<code>a</code>和<code>b</code>),那么它们就叫做“字面量字符”(literal characters)。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>/dog/.test('old dog') // true
  1597. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中正则表达式的<code>dog</code>,就是字面量字符,所以<code>/dog/</code>匹配<code>old dog</code>,因为它就表示<code>d</code>、<code>o</code>、<code>g</code>三个字母连在一起。</p> <p>除了字面量字符以外,还<strong>有一部分字符有特殊含义,不代表字面的意思。它们叫做“元字符”</strong>(metacharacters),主要有以下几个。</p> <h4 id="_1-点字符-类似通配"><a href="#_1-点字符-类似通配" class="header-anchor">#</a> (1)点字符(.) 类似通配</h4> <p>点字符(<code>.</code>)<strong>匹配除回车(<code>\r</code>)、换行(<code>\n</code>) 、行分隔符(<code>\u2028</code>)和段分隔符(<code>\u2029</code>)以外的任何单个字符</strong>。注意,对于码点大于<code>0xFFFF</code>字符,点字符不能正确匹配,会认为这是两个字符。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">c.t</span><span class="token regex-delimiter">/</span></span> <span class="token comment">//匹配c和t之间包含任意一个字符的情况,注意:一个.只匹配一个字符</span>
  1598. <span class="token string">'cet c2t c-t c.t coot c我t'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">c.t</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">)</span>
  1599. <span class="token comment">// [&quot;cet&quot;, &quot;c2t&quot;, &quot;c-t&quot;, &quot;c.t&quot;, &quot;c我t&quot;]</span>
  1600. <span class="token string">'cet c2t c-t c.t coot c我t'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">c..t</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">)</span>
  1601. <span class="token comment">//[&quot;coot&quot;]</span>
  1602. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中,<code>c.t</code>匹配<code>c</code>和<code>t</code>之间包含任意一个字符的情况,只要这三个字符在同一行,比如<code>cat</code>、<code>c2t</code>、<code>c-t</code>等等,但是不匹配<code>coot</code>。</p> <h4 id="_2-位置字符-开始位置和结束位置"><a href="#_2-位置字符-开始位置和结束位置" class="header-anchor">#</a> (2)位置字符(^) ($) 开始位置和结束位置</h4> <p>位置字符用来提示字符所处的位置,主要有两个字符。</p> <ul><li><strong><code>^</code> 表示字符串的开始位置</strong></li> <li><strong><code>$</code> 表示字符串的结束位置</strong></li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// test必须出现在开始位置</span>
  1603. <span class="token operator">/</span><span class="token operator">^</span>test<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'test123'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1604. <span class="token comment">// test必须出现在结束位置</span>
  1605. <span class="token operator">/</span>test$<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'new test'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1606. <span class="token comment">// 从开始位置到结束位置只有test</span>
  1607. <span class="token operator">/</span><span class="token operator">^</span>test$<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'test'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1608. <span class="token operator">/</span><span class="token operator">^</span>test$<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'test test'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  1609. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h4 id="_3-选择符-表示-或关系"><a href="#_3-选择符-表示-或关系" class="header-anchor">#</a> (3)选择符(<code>|</code>) 表示''或关系''</h4> <p>竖线符号(<code>|</code>)在正则表达式中表示“<strong>或关系</strong>”(OR),即<code>cat|dog</code>表示匹配<code>cat</code>或<code>dog</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">11|22</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'911'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1610. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中,正则表达式指定必须匹配<code>11</code>或<code>22</code>。</p> <p>多个选择符可以联合使用。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 匹配fred、barney、betty之中的一个</span>
  1611. <span class="token operator">/</span>fred<span class="token operator">|</span>barney<span class="token operator">|</span>betty<span class="token operator">/</span>
  1612. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>选择符会包括它前后的多个字符,比如<code>/ab|cd/</code>指的是匹配<code>ab</code>或者<code>cd</code>,而不是指匹配<code>b</code>或者<code>c</code>。如果想修改这个行为,可以使用圆括号。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a( |\t)b</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'a\tb'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1613. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码指的是,<code>a</code>和<code>b</code>之间有一个空格或者一个制表符。</p> <p>其他的元字符还包括<code>\</code>、<code>\*</code>、<code>+</code>、<code>?</code>、<code>()</code>、<code>[]</code>、<code>{}</code>等,将在下文解释。</p> <h3 id="_5-2-转义符-反斜杠"><a href="#_5-2-转义符-反斜杠" class="header-anchor">#</a> 5.2 转义符(\ 反斜杠)</h3> <p>正则表达式中那些有特殊含义的元字符,如果要匹配它们本身,就需要在它们前面要加上反斜杠。比如要匹配<code>+</code>,就要写成<code>\+</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">1+1</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'1+1'</span><span class="token punctuation">)</span>
  1614. <span class="token comment">// false</span>
  1615. <span class="token operator">/</span><span class="token number">1</span>\<span class="token operator">+</span><span class="token number">1</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'1+1'</span><span class="token punctuation">)</span>
  1616. <span class="token comment">// true</span>
  1617. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,第一个正则表达式之所以不匹配,因为加号是元字符,不代表自身。第二个正则表达式使用反斜杠对加号转义,就能匹配成功。</p> <p>正则表达式中,<strong>需要反斜杠转义的,一共有12个字符</strong>:<code>^</code>、<code>.</code>、<code>[</code>、<code>$</code>、<code>(</code>、<code>)</code>、<code>|</code>、<code>*</code>、<code>+</code>、<code>?</code>、<code>{</code>和<code>\</code>。需要特别注意的是,如果使用<code>RegExp</code>方法生成正则对象,转义需要使用两个斜杠,因为字符串内部会先转义一次。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">'1\+1'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'1+1'</span><span class="token punctuation">)</span>
  1618. <span class="token comment">// false</span>
  1619. <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">'1\\+1'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'1+1'</span><span class="token punctuation">)</span>
  1620. <span class="token comment">// true</span>
  1621. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,<code>RegExp</code>作为构造函数,参数是一个字符串。但是,在字符串内部,反斜杠也是转义字符,所以它会先被反斜杠转义一次,然后再被正则表达式转义一次,因此需要两个反斜杠转义。</p> <h3 id="_5-3-特殊字符"><a href="#_5-3-特殊字符" class="header-anchor">#</a> 5.3 特殊字符</h3> <p>正则表达式对一些不能打印的特殊字符,提供了表达方法。</p> <ul><li><p><code>\cX</code> 表示<code>Ctrl-[X]</code>,其中的<code>X</code>是A-Z之中任一个英文字母,用来匹配控制字符。</p></li> <li><p><code>[\b]</code> 匹配退格键(U+0008),不要与<code>\b</code>混淆。</p></li> <li><p><code>\n</code> 匹配换行键。</p></li> <li><p><code>\r</code> 匹配回车键。</p></li> <li><p><code>\t</code> 匹配制表符 tab(U+0009)。</p></li> <li><p><code>\v</code> 匹配垂直制表符(U+000B)。</p></li> <li><p><code>\f</code> 匹配换页符(U+000C)。</p></li> <li><p><code>\0</code> 匹配<code>null</code>字符(U+0000)。</p></li> <li><p><code>\xhh</code> 匹配一个以两位十六进制数(<code>\x00</code>-<code>\xFF</code>)表示的字符。</p></li> <li><p><code>\uhhhh</code> 匹配一个以四位十六进制数(<code>\u0000</code>-<code>\uFFFF</code>)表示的 Unicode 字符。</p></li></ul> <h3 id="_5-4-字符类"><a href="#_5-4-字符类" class="header-anchor">#</a> 5.4 字符类</h3> <p>字符类(class)<strong>表示有一系列字符可供选择,只要匹配其中一个就可以了</strong>。所有可供选择的字符都放在<strong>方括号</strong>内,比如<code>[xyz]</code> 表示<code>x</code>、<code>y</code>、<code>z</code>之中任选一个匹配。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[abc]</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'hello world'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  1622. <span class="token operator">/</span><span class="token punctuation">[</span>abc<span class="token punctuation">]</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'apple'</span><span class="token punctuation">)</span> <span class="token comment">// true a、b、c其中任意一个</span>
  1623. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,字符串<code>hello world</code>不包含<code>a</code>、<code>b</code>、<code>c</code>这三个字母中的任一个,所以返回<code>false</code>;字符串<code>apple</code>包含字母<code>a</code>,所以返回<code>true</code>。</p> <p>有两个字符在字符类中有特殊含义。</p> <h5 id="_1-脱字符-排除"><a href="#_1-脱字符-排除" class="header-anchor">#</a> (1)脱字符(^ 排除)</h5> <p>如果方括号内的第一个字符是<code>[^]</code>,则表示<strong>除了字符类之中的字符,其他字符都可以匹配</strong>。比如,<code>[^xyz]</code>表示除了<code>x</code>、<code>y</code>、<code>z</code>之外都可以匹配。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[^abc]</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'bbc news'</span><span class="token punctuation">)</span> <span class="token comment">// true 字符串中包含了a、b、c以为的字符</span>
  1624. <span class="token operator">/</span><span class="token punctuation">[</span><span class="token operator">^</span>abc<span class="token punctuation">]</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'bbc'</span><span class="token punctuation">)</span> <span class="token comment">// false 字符串中没有除了a、b、c以外的字符</span>
  1625. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,字符串<code>bbc news</code>包含<code>a</code>、<code>b</code>、<code>c</code>以外的其他字符,所以返回<code>true</code>;字符串<code>bbc</code>不包含<code>a</code>、<code>b</code>、<code>c</code>以外的其他字符,所以返回<code>false</code>。</p> <p>如果方括号内没有其他字符,即<strong>只有<code>[^]</code>,就表示匹配一切字符,其中包括换行符</strong>。相比之下,点号作为元字符(<code>.</code>)是不包括换行符的。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'Please yes\nmake my day!'</span><span class="token punctuation">;</span>
  1626. s<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">yes.*day</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// null</span>
  1627. s<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">yes[^]*day</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// [ 'yes\nmake my day']</span>
  1628. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,字符串<code>s</code>含有一个换行符,点号不包括换行符,所以第一个正则表达式匹配失败;第二个正则表达式<code>[^]</code>包含一切字符,所以匹配成功。</p> <blockquote><p>注意,脱字符只有在字符类的第一个位置才有特殊含义,否则就是字面含义。</p></blockquote> <h5 id="_2-连字符-字符连续范围"><a href="#_2-连字符-字符连续范围" class="header-anchor">#</a> (2)连字符(- 字符连续范围)</h5> <p>某些情况下,对于连续序列的字符,连字符(<code>-</code>)用来提供简写形式,表示<strong>字符的连续范围</strong>。比如,<code>[abc]</code>可以写成<code>[a-c]</code>,<code>[0123456789]</code>可以写成<code>[0-9]</code>,同理<code>[A-Z]</code>表示26个大写字母。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a-z</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'b'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  1629. <span class="token operator">/</span><span class="token punctuation">[</span>a<span class="token operator">-</span>z<span class="token punctuation">]</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'b'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1630. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,当连字号(dash)不出现在方括号之中,就不具备简写的作用,只代表字面的含义,所以不匹配字符<code>b</code>。只有当连字号用在方括号之中,才表示连续的字符序列。</p> <p>以下都是合法的字符类简写形式。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9.</span><span class="token punctuation">,</span><span class="token punctuation">]</span>
  1631. <span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span>9a<span class="token operator">-</span>fA<span class="token operator">-</span><span class="token constant">F</span><span class="token punctuation">]</span>
  1632. <span class="token punctuation">[</span>a<span class="token operator">-</span>zA<span class="token operator">-</span><span class="token constant">Z0</span><span class="token operator">-</span><span class="token number">9</span><span class="token operator">-</span><span class="token punctuation">]</span>
  1633. <span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">31</span><span class="token punctuation">]</span> <span class="token comment">// 注意:不代表1到31,只代表1到3</span>
  1634. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中最后一个字符类<code>[1-31]</code>,不代表<code>1</code>到<code>31</code>,只代表<code>1</code>到<code>3</code>。</p> <p>连字符还可以用来指定 Unicode 字符的范围。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">&quot;\u0130\u0131\u0132&quot;</span><span class="token punctuation">;</span>
  1635. <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[\u0128-\uFFFF]</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span>
  1636. <span class="token comment">// true</span>
  1637. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,<code>\u0128-\uFFFF</code>表示匹配码点在<code>0128</code>到<code>FFFF</code>之间的所有字符。</p> <p>另外,不要过分使用连字符,设定一个很大的范围,否则很可能选中意料之外的字符。最典型的例子就是<code>[A-z]</code>,表面上它是选中从大写的<code>A</code>到小写的<code>z</code>之间52个字母,但是由于在 ASCII 编码之中,大写字母与小写字母之间还有其他字符,结果就会出现意料之外的结果。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[A-z]</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'\\'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1638. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中,由于反斜杠('\')的ASCII码在大写字母与小写字母之间,结果会被选中。</p> <h3 id="_5-5-预定义模式-常见模式简写-d-d-w-w-s-s-b-b"><a href="#_5-5-预定义模式-常见模式简写-d-d-w-w-s-s-b-b" class="header-anchor">#</a> 5.5 预定义模式 (常见模式简写\d\D\w\W\s\S\b\B)</h3> <p>预定义模式指的是<strong>某些常见模式的简写方式</strong>。</p> <ul><li><code>\d</code> 匹配0-9之间的任一数字,相当于<code>[0-9]</code>。<strong>(数字)</strong></li> <li><code>\D</code> 匹配所有0-9以外的字符,相当于<code>[^0-9]</code>。<strong>(非数字)</strong></li> <li><code>\w</code> 匹配任意的字母、数字和下划线,相当于<code>[A-Za-z0-9_]</code>。<strong>(字母、数字、下划线)</strong></li> <li><code>\W</code> 除所有字母、数字和下划线以外的字符,相当于<code>[^A-Za-z0-9_]</code>。<strong>(非:字母、数字、下划线)</strong></li> <li><code>\s</code> 匹配空格(包括换行符、制表符、空格符等),相等于<code>[ \t\r\n\v\f]</code>。<strong>(空格)</strong></li> <li><code>\S</code> 匹配非空格的字符,相当于<code>[^ \t\r\n\v\f]</code>。<strong>(非空格)</strong></li> <li><code>\b</code> 匹配词的边界。<strong>(单词边界)</strong></li> <li><code>\B</code> 匹配非词边界,即在词的内部。<strong>(非单词边界)</strong></li></ul> <p>下面是一些例子。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// \s 的例子</span>
  1639. <span class="token operator">/</span>\s\w<span class="token operator">*</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'hello world'</span><span class="token punctuation">)</span> <span class="token comment">// [&quot; world&quot;]</span>
  1640. <span class="token comment">// \b 的例子</span>
  1641. <span class="token operator">/</span>\bworld<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'hello world'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1642. <span class="token operator">/</span>\bworld<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'hello-world'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1643. <span class="token operator">/</span>\bworld<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'helloworld'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  1644. <span class="token comment">// \B 的例子</span>
  1645. <span class="token operator">/</span>\Bworld<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'hello-world'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  1646. <span class="token operator">/</span>\Bworld<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'helloworld'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1647. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,<code>\s</code>表示空格,所以匹配结果会包括空格。<code>\b</code>表示词的边界,所以<code>world</code>的词首必须独立(词尾是否独立未指定),才会匹配。同理,<code>\B</code>表示非词的边界,只有<code>world</code>的词首不独立,才会匹配。</p> <p>通常,正则表达式遇到换行符(<code>\n</code>)就会停止匹配。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> html <span class="token operator">=</span> <span class="token string">&quot;&lt;b&gt;Hello&lt;/b&gt;\n&lt;i&gt;world!&lt;/i&gt;&quot;</span><span class="token punctuation">;</span>
  1648. <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">.*</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>html<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
  1649. <span class="token comment">// &quot;&lt;b&gt;Hello&lt;/b&gt;&quot;</span>
  1650. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,字符串<code>html</code>包含一个换行符,结果点字符(<code>.</code>)不匹配换行符,导致匹配结果可能不符合原意。这时使用<code>\s</code>字符类,就能包括换行符。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> html <span class="token operator">=</span> <span class="token string">&quot;&lt;b&gt;Hello&lt;/b&gt;\n&lt;i&gt;world!&lt;/i&gt;&quot;</span><span class="token punctuation">;</span>
  1651. <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[\S\s]*</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>html<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
  1652. <span class="token comment">// &quot;&lt;b&gt;Hello&lt;/b&gt;\n&lt;i&gt;world!&lt;/i&gt;&quot;</span>
  1653. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,<code>[\S\s]</code>指代一切字符。</p> <h3 id="_5-6-重复类-n-n-n-m"><a href="#_5-6-重复类-n-n-n-m" class="header-anchor">#</a> 5.6 重复类 ( {n} {n,} {n,m} )</h3> <p>模式的精确匹配次数,使用大括号(<code>{}</code>)表示。<code>{n}</code>表示恰好重复<code>n</code>次,<code>{n,}</code>表示至少重复<code>n</code>次,<code>{n,m}</code>表示重复不少于<code>n</code>次,不多于<code>m</code>次。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">lo{2}k</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'look'</span><span class="token punctuation">)</span> <span class="token comment">// true 2个o</span>
  1654. <span class="token operator">/</span>lo<span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">}</span>k<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'looook'</span><span class="token punctuation">)</span> <span class="token comment">// true 2到5个o</span>
  1655. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,第一个模式指定<code>o</code>连续出现2次,第二个模式指定<code>o</code>连续出现2次到5次之间。</p> <h3 id="_5-7-量词符"><a href="#_5-7-量词符" class="header-anchor">#</a> 5.7 量词符(?)(*)(+)</h3> <p>量词符用来设定<strong>某个模式出现的次数</strong>。</p> <ul><li><code>?</code> 问号表示某个模式出现<strong>0次或1次</strong>,等同于<code>{0, 1}</code>。</li> <li><code>*</code> 星号表示某个模式出现<strong>0次或多次</strong>,等同于<code>{0,}</code>。</li> <li><code>+</code> 加号表示某个模式出现<strong>1次或多次</strong>,等同于<code>{1,}</code>。</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// t 出现0次或1次</span>
  1656. <span class="token operator">/</span>t<span class="token operator">?</span>est<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'test'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1657. <span class="token operator">/</span>t<span class="token operator">?</span>est<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'est'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1658. <span class="token comment">// t 出现1次或多次</span>
  1659. <span class="token operator">/</span>t<span class="token operator">+</span>est<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'test'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1660. <span class="token operator">/</span>t<span class="token operator">+</span>est<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'ttest'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1661. <span class="token operator">/</span>t<span class="token operator">+</span>est<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'est'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  1662. <span class="token comment">// t 出现0次或多次</span>
  1663. <span class="token operator">/</span>t<span class="token operator">*</span>est<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'test'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1664. <span class="token operator">/</span>t<span class="token operator">*</span>est<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'ttest'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1665. <span class="token operator">/</span>t<span class="token operator">*</span>est<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'tttest'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1666. <span class="token operator">/</span>t<span class="token operator">*</span>est<span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'est'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1667. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="_5-8-贪婪模式-对量词符可使用非贪婪模式"><a href="#_5-8-贪婪模式-对量词符可使用非贪婪模式" class="header-anchor">#</a> 5.8 贪婪模式(对量词符可使用非贪婪模式)</h3> <p>上一小节的三个量词符,默认情况下都是最大可能匹配,即<strong>匹配直到下一个字符不满足匹配规则为止</strong>。这被称为贪婪模式。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'aaa'</span><span class="token punctuation">;</span>
  1668. s<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// [&quot;aaa&quot;]</span>
  1669. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,模式是<code>/a+/</code>,表示匹配1个<code>a</code>或多个<code>a</code>,那么到底会匹配几个<code>a</code>呢?因为默认是贪婪模式,会一直匹配到字符<code>a</code>不出现为止,所以匹配结果是3个<code>a</code>。</p> <p>如果想将贪婪模式改为非贪婪模式,可以在量词符后面加一个问号。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> s <span class="token operator">=</span> <span class="token string">'aaa'</span><span class="token punctuation">;</span>
  1670. s<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">a+?</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// [&quot;a&quot;]</span>
  1671. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,模式结尾添加了一个问号<code>/a+?/</code>,这时就改为<strong>非贪婪模式,一旦条件满足,就不再往下匹配</strong>。</p> <p>除了非贪婪模式的加号,还有非贪婪模式的星号(<code>*</code>)和非贪婪模式的问号(<code>?</code>)。</p> <ul><li><code>+?</code>:表示某个模式出现1次或多次,匹配时采用非贪婪模式。</li> <li><code>*?</code>:表示某个模式出现0次或多次,匹配时采用非贪婪模式。</li> <li><code>??</code>:表格某个模式出现0次或1次,匹配时采用非贪婪模式。</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token string">'abb'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">ab*b</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// [&quot;abb&quot;]</span>
  1672. <span class="token string">'abb'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">ab*?b</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// [&quot;ab&quot;]</span>
  1673. <span class="token string">'abb'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">ab?b</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// [&quot;abb&quot;]</span>
  1674. <span class="token string">'abb'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">ab??b</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// [&quot;ab&quot;]</span>
  1675. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_5-9-修饰符"><a href="#_5-9-修饰符" class="header-anchor">#</a> 5.9 修饰符</h3> <p>修饰符(modifier)表示模式的附加规则,放在正则模式的最尾部。</p> <p>修饰符可以单个使用,也可以多个一起使用。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 单个修饰符</span>
  1676. <span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">test</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">;</span>
  1677. <span class="token comment">// 多个修饰符</span>
  1678. <span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">test</span><span class="token regex-delimiter">/</span><span class="token regex-flags">ig</span></span><span class="token punctuation">;</span>
  1679. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="_1-g-修饰符-全局匹配"><a href="#_1-g-修饰符-全局匹配" class="header-anchor">#</a> (1)g 修饰符 (全局匹配)</h4> <p>默认情况下,第一次匹配成功后,正则对象就停止向下匹配了。<code>g</code>修饰符表示全局匹配(global),加上它以后,正则对象将匹配全部符合条件的结果,主要用于搜索和替换。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">b</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
  1680. <span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'abba'</span><span class="token punctuation">;</span>
  1681. regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
  1682. regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
  1683. regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
  1684. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,正则模式不含<code>g</code>修饰符,每次都是从字符串头部开始匹配。所以,连续做了三次匹配,都返回<code>true</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> regex <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">b</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
  1685. <span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'abba'</span><span class="token punctuation">;</span>
  1686. regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
  1687. regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
  1688. regex<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
  1689. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,正则模式含有<code>g</code>修饰符,每次都是从上一次匹配成功处,开始向后匹配。因为字符串<code>abba</code>只有两个<code>b</code>,所以前两次匹配结果为<code>true</code>,第三次匹配结果为<code>false</code>。</p> <h4 id="_2-i-修饰符-不区分大小写"><a href="#_2-i-修饰符-不区分大小写" class="header-anchor">#</a> (2)i 修饰符 (不区分大小写)</h4> <p>默认情况下,正则对象区分字母的大小写,加上<code>i</code>修饰符以后表示忽略大小写(ignoreCase)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">abc</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'ABC'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  1690. <span class="token operator">/</span>abc<span class="token operator">/</span>i<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'ABC'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1691. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码表示,加了<code>i</code>修饰符以后,不考虑大小写,所以模式<code>abc</code>匹配字符串<code>ABC</code>。</p> <h4 id="_3-m-修饰符-让-和-识别换行符"><a href="#_3-m-修饰符-让-和-识别换行符" class="header-anchor">#</a> (3)m 修饰符(让^和$识别换行符)</h4> <p><code>m</code>修饰符表示多行模式(multiline),会修改<code>^</code>和<code>$</code>的行为。默认情况下(即不加<code>m</code>修饰符时),<code>^</code>和<code>$</code>匹配字符串的开始处和结尾处,加上<code>m</code>修饰符以后,<code>^</code>和<code>$</code>还会匹配行首和行尾,即<code>^</code>和<code>$</code>会识别换行符(<code>\n</code>)。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">world$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'hello world\n'</span><span class="token punctuation">)</span> <span class="token comment">// false</span>
  1692. <span class="token operator">/</span>world$<span class="token operator">/</span>m<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'hello world\n'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1693. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面的代码中,字符串结尾处有一个换行符。如果不加<code>m</code>修饰符,匹配不成功,因为字符串的结尾不是<code>world</code>;加上以后,<code>$</code>可以匹配行尾。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^b</span><span class="token regex-delimiter">/</span><span class="token regex-flags">m</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'a\nb'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1694. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码要求匹配行首的<code>b</code>,如果不加<code>m</code>修饰符,就相当于<code>b</code>只能处在字符串的开始处。加上<code>m</code>修饰符以后,换行符<code>\n</code>也会被认为是一行的开始。</p> <h3 id="_5-10-组匹配"><a href="#_5-10-组匹配" class="header-anchor">#</a> 5.10 组匹配</h3> <h4 id="_1-概述-圆括号分组"><a href="#_1-概述-圆括号分组" class="header-anchor">#</a> (1)概述 (圆括号分组)</h4> <p>正则表达式的括号表示分组匹配,括号中的模式可以用来匹配分组的内容。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">fred+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'fredd'</span><span class="token punctuation">)</span> <span class="token comment">// true +加号表示重复字母d</span>
  1695. <span class="token operator">/</span><span class="token punctuation">(</span>fred<span class="token punctuation">)</span><span class="token operator">+</span><span class="token operator">/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'fredfred'</span><span class="token punctuation">)</span> <span class="token comment">// true +加号表示重复单词fred</span>
  1696. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,第一个模式没有括号,结果<code>+</code>只表示重复字母<code>d</code>,第二个模式有括号,结果<code>+</code>就表示匹配<code>fred</code>这个词。</p> <p>下面是另外一个分组捕获的例子。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> m <span class="token operator">=</span> <span class="token string">'abcabc'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(.)b(.)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1697. m
  1698. <span class="token comment">// ['abc', 'a', 'c']</span>
  1699. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,正则表达式<code>/(.)b(.)/</code>一共使用两个括号,第一个括号捕获<code>a</code>,第二个括号捕获<code>c</code>。</p> <p>注意,使用组匹配时,不宜同时使用<code>g</code>修饰符,否则<code>match</code>方法不会捕获分组的内容。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> m <span class="token operator">=</span> <span class="token string">'abcabc'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(.)b(.)</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1700. m <span class="token comment">// ['abc', 'abc']</span>
  1701. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码使用带<code>g</code>修饰符的正则表达式,结果<code>match</code>方法只捕获了匹配整个表达式的部分。这时必须使用正则表达式的<code>exec</code>方法,配合循环,才能读到每一轮匹配的组捕获。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'abcabc'</span><span class="token punctuation">;</span>
  1702. <span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(.)b(.)</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
  1703. <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1704. <span class="token keyword">var</span> result <span class="token operator">=</span> reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1705. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>result<span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span>
  1706. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1707. <span class="token punctuation">}</span>
  1708. <span class="token comment">// [&quot;abc&quot;, &quot;a&quot;, &quot;c&quot;]</span>
  1709. <span class="token comment">// [&quot;abc&quot;, &quot;a&quot;, &quot;c&quot;]</span>
  1710. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>正则表达式内部,还可以用<code>\n</code>引用括号匹配的内容,<code>n</code>是从1开始的自然数,表示对应顺序的括号。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(.)b(.)\1b\2</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">&quot;abcabc&quot;</span><span class="token punctuation">)</span>
  1711. <span class="token comment">// true</span>
  1712. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面的代码中,<code>\1</code>表示第一个括号匹配的内容(即<code>a</code>),<code>\2</code>表示第二个括号匹配的内容(即<code>c</code>)。</p> <p>下面是另外一个例子。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">y(..)(.)\2\1</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'yabccab'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1713. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>括号还可以嵌套。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">y((..)\2)\1</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'yabababab'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1714. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中,<code>\1</code>指向外层括号,<code>\2</code>指向内层括号。</p> <p>组匹配非常有用,下面是一个匹配网页标签的例子。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> tagName <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">&lt;([^&gt;]+)&gt;[^&lt;]*&lt;\/\1&gt;</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
  1715. tagName<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;b&gt;bold&lt;/b&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
  1716. <span class="token comment">// 'b'</span>
  1717. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,圆括号匹配尖括号之中的标签,而<code>\1</code>就表示对应的闭合标签。</p> <p>上面代码略加修改,就能捕获带有属性的标签。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> html <span class="token operator">=</span> <span class="token string">'&lt;b class=&quot;hello&quot;&gt;Hello&lt;/b&gt;&lt;i&gt;world&lt;/i&gt;'</span><span class="token punctuation">;</span>
  1718. <span class="token keyword">var</span> tag <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">&lt;(\w+)([^&gt;]*)&gt;(.*?)&lt;\/\1&gt;</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
  1719. <span class="token keyword">var</span> match <span class="token operator">=</span> tag<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>html<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1720. match<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment">// &quot;b&quot;</span>
  1721. match<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment">// &quot; class=&quot;hello&quot;&quot;</span>
  1722. match<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token comment">// &quot;Hello&quot;</span>
  1723. match <span class="token operator">=</span> tag<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>html<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1724. match<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment">// &quot;i&quot;</span>
  1725. match<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment">// &quot;&quot;</span>
  1726. match<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token comment">// &quot;world&quot;</span>
  1727. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h4 id="_2-非捕获组"><a href="#_2-非捕获组" class="header-anchor">#</a> (2)非捕获组</h4> <p><code>(?:x)</code>称为非捕获组(Non-capturing group),表示<strong>不返回该组匹配的内容,即匹配的结果中不计入这个括号</strong>。</p> <p>非捕获组的作用请考虑这样一个场景,假定需要匹配<code>foo</code>或者<code>foofoo</code>,正则表达式就应该写成<code>/(foo){1, 2}/</code>,但是这样会占用一个组匹配。这时,就可以使用非捕获组,将正则表达式改为<code>/(?:foo){1, 2}/</code>,它的作用与前一个正则是一样的,但是不会单独输出括号内部的内容。</p> <p>请看下面的例子。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> m <span class="token operator">=</span> <span class="token string">'abc'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?:.)b(.)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1728. m <span class="token comment">// [&quot;abc&quot;, &quot;c&quot;]</span>
  1729. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中的模式,一共使用了两个括号。其中第一个括号是非捕获组,所以最后返回的结果中没有第一个括号,只有第二个括号匹配的内容。</p> <p>下面是用来分解网址的正则表达式。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 正常匹配</span>
  1730. <span class="token keyword">var</span> url <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(http|ftp):\/\/([^/\r\n]+)(\/[^\r\n]*)?</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
  1731. url<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'http://google.com/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1732. <span class="token comment">// [&quot;http://google.com/&quot;, &quot;http&quot;, &quot;google.com&quot;, &quot;/&quot;]</span>
  1733. <span class="token comment">// 非捕获组匹配</span>
  1734. <span class="token keyword">var</span> url <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(?:http|ftp):\/\/([^/\r\n]+)(\/[^\r\n]*)?</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>
  1735. url<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'http://google.com/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1736. <span class="token comment">// [&quot;http://google.com/&quot;, &quot;google.com&quot;, &quot;/&quot;]</span>
  1737. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面的代码中,前一个正则表达式是正常匹配,第一个括号返回网络协议;后一个正则表达式是非捕获匹配,返回结果中不包括网络协议。</p> <h4 id="_3-先行断言"><a href="#_3-先行断言" class="header-anchor">#</a> (3)先行断言</h4> <p><code>x(?=y)</code>称为先行断言(Positive look-ahead)<strong>,<code>x</code>只有在<code>y</code>前面才匹配,<code>y</code>不会被计入返回结果</strong>。比如,要匹配后面跟着百分号的数字,可以写成<code>/\d+(?=%)/</code>。</p> <p>“先行断言”中,括号里的部分是不会返回的。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> m <span class="token operator">=</span> <span class="token string">'abc'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">b(?=c)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1738. m <span class="token comment">// [&quot;b&quot;]</span>
  1739. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面的代码使用了先行断言,<code>b</code>在<code>c</code>前面所以被匹配,但是括号对应的<code>c</code>不会被返回。</p> <h4 id="_4-先行否定断言"><a href="#_4-先行否定断言" class="header-anchor">#</a> (4)先行否定断言</h4> <p><code>x(?!y)</code>称为先行否定断言(Negative look-ahead),<strong><code>x</code>只有不在<code>y</code>前面才匹配,<code>y</code>不会被计入返回结果</strong>。比如,要匹配后面跟的不是百分号的数字,就要写成<code>/\d+(?!%)/</code>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\d+(?!\.)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string">'3.14'</span><span class="token punctuation">)</span>
  1740. <span class="token comment">// [&quot;14&quot;]</span>
  1741. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,正则表达式指定,只有不在小数点前面的数字才会被匹配,因此返回的结果就是<code>14</code>。</p> <p>“先行否定断言”中,括号里的部分是不会返回的。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> m <span class="token operator">=</span> <span class="token string">'abd'</span><span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">b(?!c)</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1742. m <span class="token comment">// ['b']</span>
  1743. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面的代码使用了先行否定断言,<code>b</code>不在<code>c</code>前面所以被匹配,而且括号对应的<code>d</code>不会被返回。</p> <h2 id="十一、json对象"><a href="#十一、json对象" class="header-anchor">#</a> 十一、JSON对象</h2> <h2 id="_1、json-格式"><a href="#_1、json-格式" class="header-anchor">#</a> 1、JSON 格式</h2> <p>JSON 格式(JavaScript Object Notation 的缩写)是一种用于数据交换的文本格式,2001年由 Douglas Crockford 提出,目的是取代繁琐笨重的 XML 格式。</p> <p>相比 XML 格式,JSON 格式有两个显著的优点:书写简单,一目了然;符合 JavaScript 原生语法,可以由解释引擎直接处理,不用另外添加解析代码。所以,JSON 迅速被接受,已经成为各大网站交换数据的标准格式,并被写入标准。</p> <p><strong>每个 JSON 对象就是一个值,可能是一个数组或对象,也可能是一个原始类型的值</strong>。总之,<strong>只能是一个值</strong>,不能是两个或更多的值。</p> <p>JSON 对值的类型和格式有严格的<strong>规定</strong>。</p> <blockquote><ol><li>复合类型的值只能是数组或对象,不能是函数、正则表达式对象、日期对象。</li> <li>原始类型的值只有四种:字符串、数值(必须以十进制表示)、布尔值和<code>null</code>(不能使用<code>NaN</code>, <code>Infinity</code>, <code>-Infinity</code>和<code>undefined</code>)。</li> <li>字符串必须使用双引号表示,不能使用单引号。</li> <li>对象的键名必须放在双引号里面。</li> <li>数组或对象最后一个成员的后面,不能加逗号。</li></ol></blockquote> <p>以下都是合法的 JSON。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">[</span><span class="token string">&quot;one&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;two&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;three&quot;</span><span class="token punctuation">]</span>
  1744. <span class="token punctuation">{</span> <span class="token string-property property">&quot;one&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string-property property">&quot;two&quot;</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string-property property">&quot;three&quot;</span><span class="token operator">:</span> <span class="token number">3</span> <span class="token punctuation">}</span>
  1745. <span class="token punctuation">{</span><span class="token string-property property">&quot;names&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;李四&quot;</span><span class="token punctuation">]</span> <span class="token punctuation">}</span>
  1746. <span class="token punctuation">[</span> <span class="token punctuation">{</span> <span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;李四&quot;</span><span class="token punctuation">}</span> <span class="token punctuation">]</span>
  1747. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>以下都是不合法的 JSON。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span> <span class="token string-property property">'age'</span><span class="token operator">:</span> <span class="token number">32</span> <span class="token punctuation">}</span> <span class="token comment">// 属性名必须使用双引号</span>
  1748. <span class="token punctuation">[</span><span class="token number">32</span><span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token number">128</span><span class="token punctuation">,</span> <span class="token number">0xFFF</span><span class="token punctuation">]</span> <span class="token comment">// 不能使用十六进制值</span>
  1749. <span class="token punctuation">{</span> <span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span> <span class="token string-property property">&quot;age&quot;</span><span class="token operator">:</span> <span class="token keyword">undefined</span> <span class="token punctuation">}</span> <span class="token comment">// 不能使用 undefined</span>
  1750. <span class="token punctuation">{</span> <span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span>
  1751. <span class="token string-property property">&quot;birthday&quot;</span><span class="token operator">:</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'Fri, 26 Aug 2011 07:13:10 GMT'</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  1752. <span class="token string-property property">&quot;getName&quot;</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1753. <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">;</span>
  1754. <span class="token punctuation">}</span>
  1755. <span class="token punctuation">}</span> <span class="token comment">// 属性值不能使用函数和日期对象</span>
  1756. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><strong>注意,<code>null</code>、空数组和空对象都是合法的 JSON 值。</strong></p> <h2 id="_2、json-对象"><a href="#_2、json-对象" class="header-anchor">#</a> 2、JSON 对象</h2> <p><code>JSON</code>对象是 JavaScript 的原生对象,用来处理 JSON 格式数据。它有两个静态方法:<code>JSON.stringify()</code>和<code>JSON.parse()</code>。</p> <h2 id="_3、json-stringify-转为json字符串"><a href="#_3、json-stringify-转为json字符串" class="header-anchor">#</a> 3、JSON.stringify() 转为JSON字符串</h2> <h3 id="_3-1-基本用法"><a href="#_3-1-基本用法" class="header-anchor">#</a> 3.1 基本用法</h3> <p><code>JSON.stringify</code>方法用于<strong>将一个值转为 JSON 字符串</strong>。该字符串符合 JSON 格式,并且可以被<code>JSON.parse</code>方法还原。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 单个字符串、数值、布尔值、null、空数组、空对象,都是合法json格式。</span>
  1757. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;&quot;abc&quot;&quot; 注意:字符串会转json字符串会有两对双引号</span>
  1758. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;1&quot;</span>
  1759. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token comment">// &quot;false&quot;</span>
  1760. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token comment">// &quot;null&quot;</span>
  1761. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// &quot;[]&quot;</span>
  1762. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// &quot;{}&quot;</span>
  1763. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;false&quot;</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  1764. <span class="token comment">// '[1,&quot;false&quot;,false]'</span>
  1765. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">&quot;张三&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
  1766. <span class="token comment">// '{&quot;name&quot;:&quot;张三&quot;}'</span>
  1767. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>上面代码将各种类型的值,转成 JSON 字符串。</p> <p>注意,<strong>对于原始类型的字符串,转换结果会带双引号</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token string">'foo'</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">&quot;foo&quot;</span> <span class="token comment">// false</span>
  1768. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token string">'foo'</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">&quot;\&quot;foo\&quot;&quot;</span> <span class="token comment">// true</span>
  1769. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,字符串<code>foo</code>,被转成了<code>&quot;\&quot;foo\&quot;&quot;</code>。这是因为将来还原的时候,内层双引号可以让 JavaScript 引擎知道,这是一个字符串,而不是其他类型的值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token comment">// &quot;false&quot;</span>
  1770. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token string">'false'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;\&quot;false\&quot;&quot;</span>
  1771. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,如果不是内层的双引号,将来还原的时候,引擎就无法知道原始值是布尔值还是字符串。</p> <p><strong>如果对象的属性是<code>undefined</code>、函数或 XML 对象,该属性会被<code>JSON.stringify</code>过滤</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  1772. <span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token keyword">undefined</span><span class="token punctuation">,</span>
  1773. <span class="token function-variable function">b</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  1774. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  1775. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// &quot;{}&quot; 对象属性是undefined、函数或 XML 对象会被过滤掉</span>
  1776. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,对象<code>obj</code>的<code>a</code>属性是<code>undefined</code>,而<code>b</code>属性是一个函数,结果都被<code>JSON.stringify</code>过滤。</p> <p><strong>如果数组的成员是<code>undefined</code>、函数或 XML 对象,则这些值被转成<code>null</code>。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token keyword">undefined</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  1777. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// &quot;[null,null]&quot; 数组成员是undefined、函数或 XML 对象会被转成null</span>
  1778. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,数组<code>arr</code>的成员是<code>undefined</code>和函数,它们都被转成了<code>null</code>。</p> <p><strong>正则对象会被转成空对象</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// &quot;{}&quot;</span>
  1779. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>JSON.stringify</code>方法会<strong>忽略对象的不可遍历的属性</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  1780. Object<span class="token punctuation">.</span><span class="token function">defineProperties</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  1781. <span class="token string-property property">'foo'</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  1782. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
  1783. <span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">true</span>
  1784. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  1785. <span class="token string-property property">'bar'</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  1786. <span class="token literal-property property">value</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
  1787. <span class="token literal-property property">enumerable</span><span class="token operator">:</span> <span class="token boolean">false</span>
  1788. <span class="token punctuation">}</span>
  1789. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1790. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;{&quot;foo&quot;:1}&quot;</span>
  1791. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>上面代码中,<code>bar</code>是<code>obj</code>对象的不可遍历属性,<code>JSON.stringify</code>方法会忽略这个属性。</p> <h3 id="_3-2-第二个参数-一个数组-对象属性白名单-或一个函数-改写返回值"><a href="#_3-2-第二个参数-一个数组-对象属性白名单-或一个函数-改写返回值" class="header-anchor">#</a> 3.2 第二个参数 (一个数组,对象属性白名单;或一个函数,改写返回值)</h3> <p><code>JSON.stringify</code>方法还<strong>可以接受一个数组,作为第二个参数,指定需要转成字符串的属性</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  1792. <span class="token string-property property">'prop1'</span><span class="token operator">:</span> <span class="token string">'value1'</span><span class="token punctuation">,</span>
  1793. <span class="token string-property property">'prop2'</span><span class="token operator">:</span> <span class="token string">'value2'</span><span class="token punctuation">,</span>
  1794. <span class="token string-property property">'prop3'</span><span class="token operator">:</span> <span class="token string">'value3'</span>
  1795. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  1796. <span class="token keyword">var</span> selectedProperties <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'prop1'</span><span class="token punctuation">,</span> <span class="token string">'prop2'</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 指定prop1、prop2属性转成字符串</span>
  1797. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span> selectedProperties<span class="token punctuation">)</span>
  1798. <span class="token comment">// &quot;{&quot;prop1&quot;:&quot;value1&quot;,&quot;prop2&quot;:&quot;value2&quot;}&quot;</span>
  1799. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>上面代码中,<code>JSON.stringify</code>方法的第二个参数指定,只转<code>prop1</code>和<code>prop2</code>两个属性。</p> <p>这个<strong>类似白名单的数组,只对对象的属性有效,对数组无效。</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'0'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  1800. <span class="token comment">// &quot;[&quot;a&quot;,&quot;b&quot;]&quot;</span>
  1801. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token number">0</span><span class="token operator">:</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token operator">:</span> <span class="token string">'b'</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'0'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  1802. <span class="token comment">// &quot;{&quot;0&quot;:&quot;a&quot;}&quot;</span>
  1803. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,第二个参数指定 JSON 格式只转<code>0</code>号属性,实际上对数组是无效的,只对对象有效。</p> <p>第二个参数还<strong>可以是一个函数</strong>,用来更改<code>JSON.stringify</code>的返回值。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token parameter">key<span class="token punctuation">,</span> value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 接收两个参数:键名、键值</span>
  1804. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> value <span class="token operator">===</span> <span class="token string">&quot;number&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1805. value <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> value<span class="token punctuation">;</span>
  1806. <span class="token punctuation">}</span>
  1807. <span class="token keyword">return</span> value<span class="token punctuation">;</span>
  1808. <span class="token punctuation">}</span>
  1809. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> f<span class="token punctuation">)</span>
  1810. <span class="token comment">// '{&quot;a&quot;: 2,&quot;b&quot;: 4}'</span>
  1811. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中的<code>f</code>函数,<strong>接受两个参数</strong>,分别是被转换的对象的<strong>键名和键值</strong>。如果键值是数值,就将它乘以<code>2</code>,否则就原样返回。</p> <p>注意,这个处理函数是<strong>递归处理所有的键</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> o <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  1812. <span class="token keyword">function</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token parameter">key<span class="token punctuation">,</span> value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 每一次处理的对象,都是前一次返回的值</span>
  1813. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;[&quot;</span><span class="token operator">+</span> key <span class="token operator">+</span><span class="token string">&quot;]:&quot;</span> <span class="token operator">+</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
  1814. <span class="token keyword">return</span> value<span class="token punctuation">;</span>
  1815. <span class="token punctuation">}</span>
  1816. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> f<span class="token punctuation">)</span>
  1817. <span class="token comment">// []:[object Object] 第一次键名 空,键值是o整个对象</span>
  1818. <span class="token comment">// [a]:[object Object] 第二次键名 a,键值是{b:1}</span>
  1819. <span class="token comment">// [b]:1 第三次键名 b,键值是 1</span>
  1820. <span class="token comment">// '{&quot;a&quot;:{&quot;b&quot;:1}}'</span>
  1821. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>上面代码中,对象<code>o</code>一共会被<code>f</code>函数处理三次,最后那行是<code>JSON.stringify</code>的输出。第一次键名为空,键值是整个对象<code>o</code>;第二次键名为<code>a</code>,键值是<code>{b: 1}</code>;第三次键名为<code>b</code>,键值为1。</p> <p>递归处理中,<strong>每一次处理的对象,都是前一次返回的值</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> o <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  1822. <span class="token keyword">function</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token parameter">key<span class="token punctuation">,</span> value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1823. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> value <span class="token operator">===</span> <span class="token string">'object'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1824. <span class="token keyword">return</span> <span class="token punctuation">{</span><span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  1825. <span class="token punctuation">}</span>
  1826. <span class="token keyword">return</span> value <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">;</span>
  1827. <span class="token punctuation">}</span>
  1828. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> f<span class="token punctuation">)</span>
  1829. <span class="token comment">// &quot;{&quot;b&quot;: 4}&quot;</span>
  1830. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面代码中,<code>f</code>函数修改了对象<code>o</code>,接着<code>JSON.stringify</code>方法就递归处理修改后的对象<code>o</code>。</p> <p>如果<strong>处理函数返回<code>undefined</code>或没有返回值,则该属性会被忽略</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token parameter">key<span class="token punctuation">,</span> value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1831. <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">&quot;string&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1832. <span class="token keyword">return</span> <span class="token keyword">undefined</span><span class="token punctuation">;</span>
  1833. <span class="token punctuation">}</span>
  1834. <span class="token keyword">return</span> value<span class="token punctuation">;</span>
  1835. <span class="token punctuation">}</span>
  1836. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token string">&quot;abc&quot;</span><span class="token punctuation">,</span> <span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">123</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> f<span class="token punctuation">)</span>
  1837. <span class="token comment">// '{&quot;b&quot;: 123}'</span>
  1838. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,<code>a</code>属性经过处理后,返回<code>undefined</code>,于是该属性被忽略了。</p> <h3 id="_3-3-第三个参数-增加返回json字符串的可读性-json格式"><a href="#_3-3-第三个参数-增加返回json字符串的可读性-json格式" class="header-anchor">#</a> 3.3 第三个参数 (增加返回JSON字符串的可读性;json格式)</h3> <p><code>JSON.stringify</code>还可以接受第三个参数,用于<strong>增加返回的 JSON 字符串的可读性</strong>。如果是数字,表示每个属性前面添加的空格(最多不超过10个);如果是字符串(不超过10个字符),则该字符串会添加在每行前面。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">p1</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">p2</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1839. <span class="token comment">/*
  1840. &quot;{
  1841. &quot;p1&quot;: 1,
  1842. &quot;p2&quot;: 2
  1843. }&quot;
  1844. */</span>
  1845. <span class="token comment">// 参数三是数值在使用JSON.parse()解析时正常</span>
  1846. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">p1</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">p2</span><span class="token operator">:</span><span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token string">'|-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1847. <span class="token comment">/*
  1848. &quot;{
  1849. |-&quot;p1&quot;: 1,
  1850. |-&quot;p2&quot;: 2
  1851. }&quot;
  1852. */</span>
  1853. <span class="token comment">// 参数三是字符串在使用JSON.parse()解析时报错</span>
  1854. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h3 id="_3-4-参数对象的-tojson-方法-对象自定义tojson方法-转字符串只取该方法返回值"><a href="#_3-4-参数对象的-tojson-方法-对象自定义tojson方法-转字符串只取该方法返回值" class="header-anchor">#</a> 3.4 参数对象的 toJSON 方法(对象自定义toJSON方法,转字符串只取该方法返回值)</h3> <p>如果参数对象有自定义的<code>toJSON</code>方法,那么<code>JSON.stringify</code>会<strong>使用这个方法的返回值作为参数,而忽略原对象的其他属性</strong>。</p> <p>下面是一个普通的对象。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> user <span class="token operator">=</span> <span class="token punctuation">{</span>
  1855. <span class="token literal-property property">firstName</span><span class="token operator">:</span> <span class="token string">'三'</span><span class="token punctuation">,</span>
  1856. <span class="token literal-property property">lastName</span><span class="token operator">:</span> <span class="token string">'张'</span><span class="token punctuation">,</span>
  1857. <span class="token keyword">get</span> <span class="token function">fullName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  1858. <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>lastName <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>firstName<span class="token punctuation">;</span>
  1859. <span class="token punctuation">}</span>
  1860. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  1861. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span>
  1862. <span class="token comment">// &quot;{&quot;firstName&quot;:&quot;三&quot;,&quot;lastName&quot;:&quot;张&quot;,&quot;fullName&quot;:&quot;张三&quot;}&quot;</span>
  1863. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>现在,为这个对象加上<code>toJSON</code>方法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> user <span class="token operator">=</span> <span class="token punctuation">{</span>
  1864. <span class="token literal-property property">firstName</span><span class="token operator">:</span> <span class="token string">'三'</span><span class="token punctuation">,</span>
  1865. <span class="token literal-property property">lastName</span><span class="token operator">:</span> <span class="token string">'张'</span><span class="token punctuation">,</span>
  1866. <span class="token keyword">get</span> <span class="token function">fullName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  1867. <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>lastName <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>firstName<span class="token punctuation">;</span>
  1868. <span class="token punctuation">}</span><span class="token punctuation">,</span>
  1869. <span class="token function-variable function">toJSON</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 只使用toJSON的返回值作为参数,忽略user对象的其他参数</span>
  1870. <span class="token keyword">return</span> <span class="token punctuation">{</span>
  1871. <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>lastName <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>firstName
  1872. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  1873. <span class="token punctuation">}</span>
  1874. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  1875. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span>
  1876. <span class="token comment">// &quot;{&quot;name&quot;:&quot;张三&quot;}&quot;</span>
  1877. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>上面代码中,<code>JSON.stringify</code>发现参数对象有<code>toJSON</code>方法,就直接使用这个方法的返回值作为参数,而<strong>忽略原对象的其他参数</strong>。</p> <p><code>Date</code>对象就有一个自己的<code>toJSON</code>方法。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> date <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">'2015-01-01'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1878. date<span class="token punctuation">.</span><span class="token function">toJSON</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;2015-01-01T00:00:00.000Z&quot;</span>
  1879. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>date<span class="token punctuation">)</span> <span class="token comment">// &quot;&quot;2015-01-01T00:00:00.000Z&quot;&quot;</span>
  1880. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,<code>JSON.stringify</code>发现处理的是<code>Date</code>对象实例,就会调用这个实例对象的<code>toJSON</code>方法,将该方法的返回值作为参数。</p> <p><code>toJSON</code>方法的一个应用是,<strong>将正则对象自动转为字符串</strong>。因为<code>JSON.stringify</code>默认不能转换正则对象,但是设置了<code>toJSON</code>方法以后,就可以转换正则对象了。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  1881. <span class="token literal-property property">reg</span><span class="token operator">:</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo</span><span class="token regex-delimiter">/</span></span>
  1882. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  1883. <span class="token comment">// 不设置 toJSON 方法时</span>
  1884. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token comment">// &quot;{&quot;reg&quot;:{}}&quot;</span>
  1885. <span class="token comment">// 设置 toJSON 方法时</span>
  1886. <span class="token class-name">RegExp</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>toJSON <span class="token operator">=</span> <span class="token class-name">RegExp</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>toString<span class="token punctuation">;</span>
  1887. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">foo</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token comment">// &quot;&quot;/foo/&quot;&quot;</span>
  1888. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>上面代码在正则对象的原型上面部署了<code>toJSON()</code>方法,将其指向<code>toString()</code>方法,因此转换成 JSON 格式时,正则对象就先调用<code>toJSON()</code>方法转为字符串,然后再被<code>JSON.stringify()</code>方法处理。</p> <h2 id="_4、json-parse"><a href="#_4、json-parse" class="header-anchor">#</a> 4、JSON.parse()</h2> <p><code>JSON.parse</code>方法<strong>用于将 JSON 字符串转换成对应的JSON值</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'{}'</span><span class="token punctuation">)</span> <span class="token comment">// {}</span>
  1889. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'true'</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
  1890. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'&quot;foo&quot;'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;foo&quot;</span>
  1891. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'[1, 5, &quot;false&quot;]'</span><span class="token punctuation">)</span> <span class="token comment">// [1, 5, &quot;false&quot;]</span>
  1892. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'null'</span><span class="token punctuation">)</span> <span class="token comment">// null</span>
  1893. <span class="token keyword">var</span> o <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'{&quot;name&quot;: &quot;张三&quot;}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1894. o<span class="token punctuation">.</span>name <span class="token comment">// 张三</span>
  1895. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>如果传入的字符串不是有效的 JSON 格式,<code>JSON.parse</code>方法将报错。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">&quot;'String'&quot;</span><span class="token punctuation">)</span> <span class="token comment">// illegal single quotes</span>
  1896. <span class="token comment">// SyntaxError: Unexpected token ILLEGAL</span>
  1897. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,双引号字符串中是一个单引号字符串,因为单引号字符串不符合 JSON 格式,所以报错。</p> <p>为了<strong>处理解析错误</strong>,可以将<code>JSON.parse</code>方法放在<code>try...catch</code>代码块中。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">try</span> <span class="token punctuation">{</span>
  1898. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">&quot;'String'&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1899. <span class="token punctuation">}</span> <span class="token keyword">catch</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1900. console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'parsing error'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  1901. <span class="token punctuation">}</span>
  1902. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><code>JSON.parse</code>方法<strong>可以接受一个处理函数,作为第二个参数,用法与<code>JSON.stringify</code>方法类似</strong>。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token parameter">key<span class="token punctuation">,</span> value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">// 参数:键名、键值</span>
  1903. <span class="token keyword">if</span> <span class="token punctuation">(</span>key <span class="token operator">===</span> <span class="token string">'a'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  1904. <span class="token keyword">return</span> value <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">;</span>
  1905. <span class="token punctuation">}</span>
  1906. <span class="token keyword">return</span> value<span class="token punctuation">;</span>
  1907. <span class="token punctuation">}</span>
  1908. <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">'{&quot;a&quot;: 1, &quot;b&quot;: 2}'</span><span class="token punctuation">,</span> f<span class="token punctuation">)</span>
  1909. <span class="token comment">// {a: 11, b: 2}</span>
  1910. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,<code>JSON.parse</code>的第二个参数是一个函数,如果键名是<code>a</code>,该函数会将键值加上10。</p> <h2 id="文档"><a href="#文档" class="header-anchor">#</a> 文档</h2> <p>学习文档:<a href="https://wangdoc.com/javascript/" target="_blank" rel="noopener noreferrer">https://wangdoc.com/javascript/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></div></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/heBody/blog/edit/master/docs/《JavaScript教程》笔记/02.内置对象.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/blog/tags/?tag=JavaScript" title="标签">#JavaScript</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2022/12/14, 19:36:42</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/blog/pages/0796ba76b4b55368/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">基础</div></a> <a href="/blog/pages/659b5af5e2e704e0/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">面向对象</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
  1911. <a href="/blog/pages/0796ba76b4b55368/" class="prev">基础</a></span> <span class="next"><a href="/blog/pages/659b5af5e2e704e0/">面向对象</a>→
  1912. </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/blog/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/blog/pages/922650/"><div>
  1913. Git修改分支名
  1914. <!----></div></a> <span class="date">08-11</span></dt></dl><dl><dd>02</dd> <dt><a href="/blog/pages/55f894/"><div>
  1915. CSS给table的tbody添加滚动条
  1916. <!----></div></a> <span class="date">06-29</span></dt></dl><dl><dd>03</dd> <dt><a href="/blog/pages/829589/"><div>
  1917. 我做了一个手写春联小网页,祝大家虎年暴富
  1918. <span class="title-tag">
  1919. 原创
  1920. </span></div></a> <span class="date">01-28</span></dt></dl> <dl><dd></dd> <dt><a href="/blog/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:30363811@qq.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/heBody" title="GitHub" target="_blank" class="iconfont icon-github"></a></div>
  1921. Copyright © 2016-2022
  1922. <span>Hesb | <a href="https://github.com/heBody/blob" target="_blank">MIT License</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
  1923. 跟随系统
  1924. </li><li class="iconfont icon-rijianmoshi">
  1925. 浅色模式
  1926. </li><li class="iconfont icon-yejianmoshi">
  1927. 深色模式
  1928. </li><li class="iconfont icon-yuedu">
  1929. 阅读模式
  1930. </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
  1931. <script src="/blog/assets/js/app.28edfb28.js" defer></script><script src="/blog/assets/js/2.395c0d18.js" defer></script><script src="/blog/assets/js/3.6748bd5c.js" defer></script><script src="/blog/assets/js/137.71e8757e.js" defer></script>
  1932. </body>
  1933. </html>