外文文献与翻译.docx
- 文档编号:15995868
- 上传时间:2023-07-09
- 格式:DOCX
- 页数:10
- 大小:23.70KB
外文文献与翻译.docx
《外文文献与翻译.docx》由会员分享,可在线阅读,更多相关《外文文献与翻译.docx(10页珍藏版)》请在冰点文库上搜索。
外文文献与翻译
JavaScript
WhenIwasayoungjourneymanprogrammer,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,JavaScriptbecamethe"LanguageoftheWeb"bydefault.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.
AnalyzingJavaScript
JavaScriptisbuiltonsomeverygoodideasandafewverybadones.
Theverygoodideasincludefunctions,loosetyping,dynamicobjects,andanexpressiveobjectliteralnotation.Thebadideasincludeaprogrammingmodelbasedonglobalvariables.
JavaScript'sfunctionsarefirstclassobjectswith(mostly)lexicalscoping.JavaScriptisthefirstlambdalanguagetogomainstream.Deepdown,JavaScripthasmoreincommonwithLispandSchemethanwithJava.ItisLispinC'sclothing.ThismakesJavaScriptaremarkablypowerfullanguage.
Thefashioninmostprogramminglanguagestodaydemandsstrongtyping.Thetheoryisthatstrongtypingallowsacompilertodetectalargeclassoferrorsatcompiletime.Thesoonerwecandetectandrepairerrors,thelesstheycostus.JavaScriptisalooselytypedlanguage,soJavaScriptcompilersareunabletodetecttypeerrors.ThiscanbealarmingtopeoplewhoarecomingtoJavaScriptfromstronglytypedlanguages.Butitturnsoutthatstrongtypingdoesnoteliminatetheneedforcarefultesting.AndIhavefoundinmyworkthatthesortsoferrorsthatstrongtypecheckingfindsarenottheerrorsIworryabout.Ontheotherhand,Ifindloosetypingtobeliberating.Idon'tneedtoformcomplexclasshierarchies.AndIneverhavetocastorwrestlewiththetypesystemtogetthebehaviorthatIwant.
JavaScripthasaverypowerfulobjectliteralnotation.Objectscanbecreatedsimplybylistingtheircomponents.ThisnotationwastheinspirationforJSON,thepopulardatainterchangeformat.
AcontroversialfeatureinJavaScriptisprototypalinheritance.JavaScripthasaclass-freeobjectsysteminwhichobjectsinheritpropertiesdirectlyfromotherobjects.Thisisreallypowerful,butitisunfamiliartoclassicallytrainedprogrammers.IfyouattempttoapplyclassicaldesignpatternsdirectlytoJavaScript,youwillbefrustrated.ButifyoulearntoworkwithJavaScript'sprototypalnature,youreffortswillberewarded.
JavaScriptismuchmalignedforitschoiceofkeyideas.Forthemostpart,though,thosechoicesweregood,ifunusual.Buttherewasonechoicethatwasparticularlybad:
JavaScriptdependsonglobalvariablesforlinkage.Allofthetop-levelvariablesofallcompilationunitsaretossedtogetherinacommonnamespacecalledtheglobalobject.Thisisabadthingbecauseglobalvariablesareevil,andinJavaScripttheyarefundamental.Fortunately,aswewillsee,JavaScriptalsogivesusthetoolstomitigatethisproblem.
Inafewcases,wecan'tignorethebadparts.Therearesomeunavoidableawfulparts,whichwillbecalledoutastheyoccur.TheywillalsobesummarizedinAppendixA.Butwewillsucceedinavoidingmostofthebadpartsinthisbook,summarizingmuchofwhatwasleftoutinAppendixB.Ifyouwanttolearnmoreaboutthebadpartsandhowtousethembadly,consultanyotherJavaScriptbook.
ThestandardthatdefinesJavaScript(akaJScript)isthethirdeditionofTheECMAScriptProgrammingLanguage.ThelanguagedescribedinthisbookisapropersubsetofECMAScript.Thisbookdoesnotdescribethewholelanguagebecauseitleavesoutthebadparts.Thetreatmenthereisnotexhaustive.Itavoidstheedgecases.Youshould,too.Thereisdangerandmiseryattheedges.
AJavaScriptparserthatcananalyzeaJavaScriptprogramandreportonthebadpartsthatitcontains.JSLintprovidesadegreeofrigorthatisgenerallylackinginJavaScriptdevelopment.Itcangiveyouconfidencethatyourprogramscontainonlythegoodparts.
JavaScriptisalanguageofmanycontrasts.Itcontainsmanyerrorsandsharpedges,soyoumightwonder,"WhyshouldIuseJavaScript?
"Therearetwoanswers.Thefirstisthatyoudon'thaveachoice.TheWebhasbecomeanimportantplatformforapplicationdevelopment,andJavaScriptistheonlylanguagethatisfoundinallbrowsers.ItisunfortunatethatJavafailedinthatenvironment;ifithadn't,therecouldbeachoiceforpeopledesiringastronglytypedclassicallanguage.ButJavadidfailandJavaScriptisflourishing,sothereisevidencethatJavaScriptdidsomethingright.
Theotheransweristhat,despiteitsdeficiencies,JavaScriptisreallygood.Itislightweightandexpressive.Andonceyougetthehangofit,functionalprogrammingisalotoffun.
Butinordertousethelanguagewell,youmustbewellinformedaboutitslimitations.Iwillpoundonthosewithsomebrutality.Don'tletthatdiscourageyou.Thegoodpartsaregoodenoughtocompensateforthebadparts.
Style
Computerprogramsarethemostcomplexthingsthathumansmake.Programsaremadeupofahugenumberofparts,expressedasfunctions,statements,andexpressionsthatarearrangedinsequencesthatmustbevirtuallyfreeoferror.Theruntimebehaviorhaslittleresemblancetotheprogramthatimplementsit.Softwareisusuallyexpectedtobemodifiedoverthecourseofitsproducti
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 外文 文献 翻译