1. 首页
  2. 编程语言
  3. Javascript
  4. 编写可维护的Javascript

编写可维护的Javascript

上传者: 2019-09-24 01:32:29上传 PDF文件 8.78MB 热度 18次
编写可维护的JavaScript》向开发人员阐述了如何在团队开发中编写具备高可维护性的JavaScript代码,书中详细说明了作为团队一分子,应该怎么写JavaScript。《编写可维护的JavaScript》内容涵盖了编码风格、编程技巧、自动化、测试等几方面,既包括具体风格和原则的介绍,也包括示例和技巧说明,最后还介绍了如何通过自动化的工具和方法来实现一致的编程风格。《编写可维护的JavaScript》适合前端开发工程师、JavaScript程序员和学习JavaScript编程的读者阅读,也适合开发团队负责人、项目负责人阅读。Maintainable javaScriptNicholas c. zakas○ REILLYBeijing· Cambridge: Farnham·Koln· Sebastopol· TokyoMaintainable javaScriptby nicholas C. ZakasCopyright o 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 dolbIllustrator: Rebecca demarestMay 2012First editionRevision history for the First Edition:201205-09First releaseSeehttp://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 astrademarks. Where those designations appear in this book, and O Reilly mediawas 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 con-tained hereinISBN:978-1-449-32768-2ILSI1336581452Table of contentsntroduction∴PrefacePart L. Style guidelines1. Basic Formatting∴Indentation levelsStatement Terminationine lengLine breakingBlank i10Ing11Variables and functionsConstants13Constructors13Literal values14Stringsber1516Undefined17Object Literals18Array2.〔 omments∴21Single-Line commentsMultiline comments23Using CommentsDifficult-to-Understand CodePotential Author errors25Browser-Specific HacksDocumentation Comments273. Statements and expressions....................... 29Brace alig30Block Statement spacing31The switch statement31Indentation32Faalling Ihrough33default34The with statement35The for Loop35ne for-in loo4. Variables, Functions, and Operators.,.................. 39Variable declarations39Function DeclarationsFunction Call spacer42Immediate Function Invocation43Strict mode44EqualityPrimitive wrapper types48Part II. Programming Practices5. Loose Coupling of UI LayersWhat Is Loose coupling54Keep JavaScript Out of cssKeep Css Out of JavaScriptKeep JavaScript Out of HTMLKeep hTml Out of Java Script9Alternative #1: Load from the Server60Alternative #2: Simple Client-Side TemplatesAlternative #3: Complex Client-Side templates636. Avoid globals∴,,,67The Problems with globals67Naming CollisionsCode fragilityDifficulty Testing69Accidental globals69ⅳ| Table of contentsAvoiding Accidental globals70The One-Global ApproacNamespacesModules74The Zero-Global Approach7. Event Handling............................ 79Classic Usage79Rule #1: Separate application logic80Rule #2: Dont Pass the Event object around818. Avoid Null Comparisons83Detecting Primitive valuesDetecting Reference valuesDetecting functionsDetecting Arrays88Detecting Properties899. Separate Configuration Data from Code.............. .... 91What Is Configuration Data?91Externalizing Configuration Data92Storing Configuration Data9310. Throw your own errors95The Nature of errors95Throwing Errors in JavaScript96Advantages of Throwing errors97When to Throw errors97The try-catch StatementThrow or try-catch?100Error types10011. Don't Modify objects You Dont Own..............What Do You own?103The rules104Dont Override Methods104Dont Add New Methods105Dont Remove methods107etter Appi108Object-Based Inheritance108Type-Based Inheritance109The Facade pattern110A Note on polyfillsTable of contentsPreventing Modification11212. Browser detection115r-Agent Detection115Feature detection117Avoid Feature Inference119Avoid browser inference120What Should You use?122PartⅢ. automation13. File and Directory Structure.127Best practices127Basic layout12814. Ant133Installation133The Build file133Running the build134Target Dependencies135Properties136Build13715. Validation∴,,139Finding files139The taskImproving the target141Other Improvements142BIullar las14316. Concatenation and Baking....................... 145The Task145Line End146Headers and footers147Baking file14817. Minification and Compression,151unification151Minifying with YUI Compressor15Minifying with Closure Compiler154Minifying with UglifyJS156ompression157vi Table of ContentsRuntime compressionBuild-Time Compression15818. Documentation,,161JSDoc Toolkit161YUI DoC16319. Automated TestingYUI Test Selenium Driver167betting Up a Selenium Server168Setting Up YUI Test Selenium Driver168Using the YUI Test Selenium Driver168The ant Target171PhantomSInstallation and Usage172The ant TargetJsTestDInstallation and Usage174The Ant Target17420. Putting It Togethe177Missing Pieces177Planning the build178The Development build179The Integration BuildThe Release build180USing a CI System181Jenkins181Other Ci systems184A. JavaScript Style Guide,185B. JavaScript tools205Index209Table of contents|ⅶi
下载地址
用户评论