JavaScriptWhenIwasayoungjourneymanprogrammer,IwouldlearnabouteveryfeatureofthelanguagesIwasusing,andIwouldattempttouseallofthosefeatureswhenIwrote.Isupposeitwasawayofshowingoff,andIsupposeitworkedbecauseIwastheguyyouwenttoifyouwantedtoknowhowtouseaparticularfeature.EventuallyIfiguredoutthatsomeofthosefeaturesweremoretroublethantheywereworth.Someofthemwerepoorlyspecified,andsoweremorelikelytocauseportabilityproblems.Someresultedincodethatwasdifficulttoreadormodify.Someinducedmetowriteinamannerthatwastootrickyanderror-prone.Andsomeofthosefeaturesweredesignerrors.Sometimeslanguagedesignersmakemistakes.Mostprogramminglanguagescontaingoodpartsandbadparts.IdiscoveredthatIcouldbeabetterprogrammerbyusingonlythegoodpartsandavoidingthebadparts.Afterall,howcanyoubuildsomethinggoodoutofbadparts?Itisrarelypossibleforstandardscommitteestoremoveimperfectionsfromalanguagebecausedoingsowouldcausethebreakageofallofthebadprogramsthatdependonthosebadparts.Theyareusuallypowerlesstodoanythingexceptheapmorefeaturesontopoftheexistingpileofimperfections.Andthenewfeaturesdonotalwaysinteractharmoniously,thusproducingmorebadparts.Butyouhavethepowertodefineyourownsubset.Youcanwritebetterprogramsbyrelyingexclusivelyonthegoodparts.JavaScriptisalanguagewithmorethanitsshareofbadparts.Itwentfromnon-existencetoglobaladoptioninanalarminglyshortperiodoftime.Itneverhadanintervalinthelabwhenitcouldbetriedoutandpolished.ItwentstraightintoNetscapeNavigator2justasitwas,anditwasveryrough.WhenJava™appletsfailed,JavaScriptbecametheLanguageoftheWebbydefault.JavaScript'spopularityisalmostcompletelyindependentofitsqualitiesasaprogramminglanguage.Fortunately,JavaScripthassomeextraordinarilygoodparts.InJavaScript,thereisabeautiful,elegant,highlyexpressivelanguagethatisburiedunderasteamingpileofgoodintentionsandblunders.ThebestnatureofJavaScriptissoeffectivelyhiddenthatformanyyearstheprevailingopinionofJavaScriptwasthatitwasanunsightly,incompetenttoy.MyintentionhereistoexposethegoodnessinJavaScript,anoutstanding,dynamicprogramminglanguage.JavaScriptisablockofmarble,andIchipawaythefeaturesthatarenotbeautifuluntilthelanguage'struenaturerevealsitself.IbelievethattheelegantsubsetIcarvedoutisvastlysuperiortothelanguageasawhole,beingmorereliable,readable,andmaintainable.Thisbookwillnotattempttofullydescribethelanguage.Instead,itwillfocusonthegoodpartswithoccasionalwarningstoavoidthebad.Thesubsetthatwillbedescribedherecanbeusedtoconstructreliable,readableprogramssmallandlarge.Byfocusingonjustthegoodparts,wecanreducelearningtime,increaserobustness,andsavesometrees.Perhapsthegreatestbenefitofstudyingthegoodpartsisthatyoucanavoidtheneedtounlearnthebadparts.Unlearningbadpatternsisverydifficult.Itisapainfultaskthatmostofusfacewithextremereluctance.Sometimeslanguagesaresubsettedtomakethemworkbetterforstudents.Butinthiscase,IamsubsettingJavaScripttomakeitworkbetterforprofessionals.WhyJavaScript?JavaScriptisanimportantlanguagebecauseitisthelanguageofthewebbrowser.Itsassociationwiththebrowsermakesitoneofthemostpopularprogramminglanguagesintheworld.Atthesametime,itisoneofthemostdespisedprogramminglanguagesintheworld.TheAPIofthebrowser,theDocumentObjectModel(DOM)isquiteawful,andJavaScriptisunfairlyblamed.TheDOMwouldbepainfultoworkwithinanylanguage.TheDOMispoorlyspecifiedandinconsistentlyimplemented.ThisbooktouchesonlyverylightlyontheDOM.IthinkwritingaGoodPartsbookabouttheDOMwouldbeextremelychallenging.JavaScriptismostdespisedbecauseitisn'tSOMEOTHERLANGUAGE.IfyouaregoodinSOMEOTHERLANGUAGEandyouhavetoprograminanenvironmentthatonlysupportsJavaScript,thenyouareforcedtouseJavaScript,andthatisannoying.Mostpeopleinthatsituationdon'tevenbothertolearnJavaScriptfirst,andthentheyaresurprisedwhenJavaScriptturnsouttohavesignificantdifferencesfromtheSOMEOTHERLANGUAGEtheywouldratherbeusing,andthatthosedifferencesmatter.TheamazingthingaboutJavaScriptisthatitispossibletogetworkdonewithitwithoutknowingmuchaboutthelanguage,orevenknowingmuchaboutprogramming.Itisalanguagewithenormousexpressivepower.Itisevenbetterwhenyouknowwhatyou'redoing.Programmingisdifficultbusiness.Itshouldneverbeundertakeninignorance.AnalyzingJavaScriptJavaScriptisbuiltonsomeverygoodideasandafewverybadones.Theverygoodideasincludefunctions,loosetyping,dynamicobjects,andanexpressiveobjectliteralnotation.Thebadideasincludeaprogrammingmodelbasedonglobalvariables.JavaScript'sfunctionsarefirstclassobjectswith(mostly)lexicalscoping.JavaScriptisthefirstlambdalanguagetogomainstream.Deepdown,JavaScripthasmoreincommonwithLispandSchemethanwithJava.ItisLispinC'sclothing.ThismakesJavaScriptaremarkablypowerfullanguage.Thefashioninmostprogramminglanguagestodaydemandsstrongtyping.Thetheoryisthatstrongtypingallowsacompilertodetectalargeclassoferrorsatcompiletime.Thesoonerwecandetectandrepairerrors,thelesstheycostus.JavaScriptisalooselytypedlanguage,soJavaScriptcompilersareunabletodetecttypeerrors.ThiscanbealarmingtopeoplewhoarecomingtoJavaScriptfromstronglytypedlanguages.Butitturnsoutthatstrongtypingdoesnoteliminatetheneedforcarefultesting.A