资源描述:
《how_to_write_jquery_plugins》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、BLOGABOUTHowtowritejQuerypluginsRSSFeedsPosted1day,9hoursagobyTimKoschützkiBLOGRSSCOMMENTSRSSjQuery,themostpopularjavascriptlibraryoutthere,isgreatforDOMabstraction.Itallowsyoutoencapsulatefunctionalityintoyourownplugins,whichisagreatwaytowritereusablecode.However,jQuery'srulesforwritingplu
2、ginsareveryloose,whichleadstodifferentplugindevelopmentpractices-someofwhichareprettypoor.WiththisarticleIwanttoprovideasimpleplugindevelopmentpatternthatwillworkinRecentPostsmanysituations.Ifthefunctionalityyouwouldliketoencapsulateislargeandreallycomplex,jQuerypluginsareprobablynotwhatyou
3、shoulduseinthefirstplace.You'dHowtowritejQuerypluginsratherusesomethinglikeBackboneJSorjQuery.Controllerinthiscase.VimWorkshopinBerlin(April20)Ifyoucan'tordon'twanttouseBackbone,youmightstillgetawaywithmysolution...NPM-AninterventionTestingnode.jsmoduleswithopeninbrowserPROversionAreyouadev
4、eloper?TryouttheHTMLtoPDFAPINewhotapp:FacebookAlbumsToPDFpdfcrowd.comTestingnode.jsmoduleswithStartingoffTravisCIPrivatenpmmodules;(function($,doc,win){Howtofork&patchnpm"usestrict";modules//plugincodewillcomehereArchive})(jQuery,document,window);442Posts,4600CommentsThesemi-colonbeforethef
5、unctioninvocationkeepsthepluginfrombreakingifourpluginisconcatenatedwithotherscriptsthatarenotclosedproperly.Weareonairsince2006.Takeatourthroughtheblogarchive."usestrict";putsourcodeintostrictmode,whichcatchessomecommoncodingSearchproblemsbythrowingexceptions,prevents/throwserrorswhenrelat
6、ively"unsafe"actionsaretakenanddisablesJavascriptfeaturesthatareconfusingorpoorlythoughtout.Toreadaboutthisindetail,pleasecheckECMAScript5StrictMode,JSON,andRecentCommentsMorebyJohnResig.Thanksforreportingt..WrappingthejQueryobjectintothedollarsignviaaclosureavoidsconflictswithotherTim,Than
7、ksforthi..librariesthatalsousethedollarsignasanabbreviation.windowanddocumentareReallynice!Thankyou..passedthroughaslocalvariablesratherthanasglobals,becausethisspeedsuptheHiTim,TheURLof..resolutionprocessandcanbemoreefficientlyminified.Thepatternyoudesc