编写可维护的JavaScript.pdf
JavaScript 学习讲义 编写可维护的JavaScript.pdfMaintainable JavascriptNicholas c. zakasO REILLYBeijing· Cambridge· Farnham·Koln· Sebastopol· TokyoMaintainable JavaScriptby nicholas C. ZakasCopyright@ 2012 Nicholas Zakas. All rights reservedPrinted in the United States of americaPublished by O Reilly Media, Inc, 1005 Gravenstein Highway North, Sebastopol, CA 95472O'Reilly books may be purchased for educational, business, or sales promotional use. Online editionsarealsoavailableformosttitles(http://my.safaribooksonline.com).Formoreinformationcontactourcorporate/institutionalsalesdepartment800-998-9938orcorporate@oreilly.comEditor: Mary treselerIndexer: Lucie haskinsProduction Editor: Holly bauerCover designer Karen montgomeryCopyeditor: Nancy RotaryInterior Designer: David FutatoProofreader Linley dolbyllustrator: Rebecca demarestMay 2012First editionRevision history for the first Edition:201205-09cleaseSeehttp:/oreilly.com/catalog/errata.csp?isbn=9781449327682forreleasedetailsNutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks ofO'Reilly Media, Inc. Maintainable JavaScript, the image of a Greek tortoise, and related trade dress aretrademarks of O'Reilly Media, IncMany of the designations used by manufacturers and sellers to distinguish their products are claimed asrademarks. Where those designations appear in this book, and O Reilly Media, Inc, was aware of atrademark claim, the designations have been printed in caps or initial capsWhile every precaution has been taken in the preparation of this book, the publisher and authors assumeno responsibility for errors or omissions, or for damages resulting from the use of the information contained hereinISBN:978-1-449-32768-2ILSI1336581452Table of contentsIntroductionPrefaceXIlPart L. Style Guidelines1. Basic formattingIndentation LevelsStatement terminationLine lengthgLine breakingBlank l10Naming11Variables and functionsonstantsConstructors13Literal values14Strings14Number1516UndefinedObject literalsArray Ly2.〔 omments∴21Single-Line CommentsMultiline comments23Using comments24Difficult-to-Understand Code25Potential Author errors5Browser-Specific HacksDocumentation comments3. Statements and expressionsBrace Alignment30Block Statement Spacing31The switch Statement31Indentation32Falling through33default34he with Statement35The for LoopThe for-in LoopVariables, Functions, and OperatorsVariable declarations39Function declarationsFunction Call Spacing42Immediate Function invocationStrict mode44Equality444Primitive Wrapper Types578Part IL. Programming Practices5. Loose Coupling of UI LayersWhat Is loose coupling54eep JavaScript Out of CssKeep Css Out of JavaScript56Keep JavaScript Out of HTmLKeep html Out of Java Script59Alternative #1 Load from the server60Alternative #2 Simple client-Side templates61Alternative #3: Complex Client-Side Templates636. Avoid globals∴,67The Problems with globals67Naming collisionsCode fragilityDifficulty Testing69Accidental globals69iv Table of ContentsAvoiding Accidental Globals70The One-Global ApproachamespacesModules74The Zero-Global Approach7. Event Handling79Classic usage79Rule #1: Separate application logic80Rule #2 Dont Pass the Event object around818. Avoid Null comparisons83Detecting primitive valuesDetecting Reference valuesDetecting FunctionsDetecting Arrays88Detecting properties9. Separate Configuration Data from Code91What Is Configuration Data?91Externalizing Configuration Data92Storing Configuration Data9310. Throw your own errors95he Nature of errors95Throwing Errors in JavaScriptAdvantages of Throwing errors97When to throw errors97he try-catch StatementThrow or try-catch100Error types10011. Don't Modify Objects You Don't Own........What Do You own?103The ri104Don't Override Methods104Dont Add New methods105Don't Remove metheBetter Approaches108Object-Based Inheritance108Type-Based Inheritance109The Facade pattern110A Note on polyfillsTable of Contents vPreventing Modification12. Browser detection115User-Agent detect115re detection117Avoid Feature Inference119Avoid browser Infe120What Should You use?122PartⅢ. Automation13. File and Directory Structure127Best practices127Basic layout14. Ant133nstallation133The build file133Running the buildTarget Dependencies135Properties136Build13715. Validation139The Task140Improving the target141Other Improvements142Buildr Task14316. Concatenation and Baking...,.........145The task145Line end146eaders and footers147Baking file14817. Minification and Compression,151Minification151Minifying with YUI Compressor152Minifying with closure compiler154Minifying with uglifygJS156Compression157ⅵ i Table of ContentsRuntime compression157Build-Time Compression15818. Documentation161JSDoc Toolkit161YUI DoC16319. Automated TestingYUI Test Selenium driver167Setting up a selenium Server168Setting Up YUI Test Selenium Driver168Using the YUI Test Selenium Driver168The ant targetY171PhantomSInstallation and Usage172The ant TargetJsTestDInstallation and Usage174The ant target174Putting It Togethe177Missing pieces177Planning the build178The Development Build179The Integration Build180The Release build180sing agCI System181elkins181ther CI Systems184A. JavaScript Style Guide185B. JavaScript Tools205Index209Table of contents|ⅶi
用户评论
高清文字版的,但是是英文版