Maintainable JavaScript

Book description

You may have definite ideas about writing code when working alone, but team development requires that everyone use the same approach. With the JavaScript practices in this book—including code style, programming tips, and automation—you will learn how to write maintainable code that other team members can easily understand, adapt, and extend. Author Nicholas Zakas assembled this collection of best practices as a front-end tech leader at Yahoo!, after completing his own journey from solo hacker to team player.

Publisher resources

View/Submit Errata

Table of contents

  1. Introduction
  2. Preface
    1. Conventions Used in This Book
    2. Using Code Examples
    3. Safari® Books Online
    4. How to Contact Us
  3. I. Style Guidelines
    1. 1. Basic Formatting
      1. Indentation Levels
      2. Statement Termination
      3. Line Length
      4. Line Breaking
      5. Blank Lines
      6. Naming
        1. Variables and Functions
        2. Constants
        3. Constructors
      7. Literal Values
        1. Strings
        2. Numbers
        3. Null
        4. Undefined
        5. Object Literals
        6. Array Literals
    2. 2. Comments
      1. Single-Line Comments
      2. Multiline Comments
      3. Using Comments
        1. Difficult-to-Understand Code
        2. Potential Author Errors
        3. Browser-Specific Hacks
      4. Documentation Comments
    3. 3. Statements and Expressions
      1. Brace Alignment
      2. Block Statement Spacing
      3. The switch Statement
        1. Indentation
        2. Falling Through
        3. default
      4. The with Statement
      5. The for Loop
      6. The for-in Loop
    4. 4. Variables, Functions, and Operators
      1. Variable Declarations
      2. Function Declarations
      3. Function Call Spacing
      4. Immediate Function Invocation
        1. Strict Mode
      5. Equality
      6. eval()
      7. Primitive Wrapper Types
  4. II. Programming Practices
    1. 5. Loose Coupling of UI Layers
      1. What Is Loose Coupling?
      2. Keep JavaScript Out of CSS
      3. Keep CSS Out of JavaScript
      4. Keep JavaScript Out of HTML
      5. Keep HTML Out of JavaScript
        1. Alternative #1: Load from the Server
        2. Alternative #2: Simple Client-Side Templates
        3. Alternative #3: Complex Client-Side Templates
    2. 6. Avoid Globals
      1. The Problems with Globals
        1. Naming Collisions
        2. Code Fragility
        3. Difficulty Testing
      2. Accidental Globals
        1. Avoiding Accidental Globals
      3. The One-Global Approach
        1. Namespaces
        2. Modules
          1. YUI modules
          2. Asynchronous Module Definition (AMD) Modules
      4. The Zero-Global Approach
    3. 7. Event Handling
      1. Classic Usage
      2. Rule #1: Separate Application Logic
      3. Rule #2: Don’t Pass the Event Object Around
    4. 8. Avoid Null Comparisons
      1. Detecting Primitive Values
      2. Detecting Reference Values
        1. Detecting Functions
        2. Detecting Arrays
      3. Detecting Properties
    5. 9. Separate Configuration Data from Code
      1. What Is Configuration Data?
      2. Externalizing Configuration Data
      3. Storing Configuration Data
    6. 10. Throw Your Own Errors
      1. The Nature of Errors
      2. Throwing Errors in JavaScript
      3. Advantages of Throwing Errors
      4. When to Throw Errors
      5. The try-catch Statement
        1. Throw or try-catch?
      6. Error Types
    7. 11. Don’t Modify Objects You Don’t Own
      1. What Do You Own?
      2. The Rules
        1. Don’t Override Methods
        2. Don’t Add New Methods
        3. Don’t Remove Methods
      3. Better Approaches
        1. Object-Based Inheritance
        2. Type-Based Inheritance
        3. The Facade Pattern
      4. A Note on Polyfills
      5. Preventing Modification
    8. 12. Browser Detection
      1. User-Agent Detection
      2. Feature Detection
      3. Avoid Feature Inference
      4. Avoid Browser Inference
      5. What Should You Use?
  5. III. Automation
    1. 13. File and Directory Structure
      1. Best Practices
      2. Basic Layout
    2. 14. Ant
      1. Installation
      2. The Build File
      3. Running the Build
      4. Target Dependencies
      5. Properties
      6. Buildr
    3. 15. Validation
      1. Finding Files
      2. The Task
      3. Improving the Target
      4. Other Improvements
      5. Buildr Task
    4. 16. Concatenation and Baking
      1. The Task
      2. Line Endings
      3. Headers and Footers
      4. Baking Files
    5. 17. Minification and Compression
      1. Minification
        1. Minifying with YUI Compressor
        2. Minifying with Closure Compiler
        3. Minifying with UglifyJS
      2. Compression
        1. Runtime Compression
        2. Build-Time Compression
    6. 18. Documentation
      1. JSDoc Toolkit
      2. YUI Doc
    7. 19. Automated Testing
      1. YUI Test Selenium Driver
        1. Setting Up a Selenium Server
        2. Setting Up YUI Test Selenium Driver
        3. Using the YUI Test Selenium Driver
        4. The Ant Target
      2. Yeti
      3. PhantomJS
        1. Installation and Usage
        2. The Ant Target
      4. JsTestDriver
        1. Installation and Usage
        2. The Ant Target
    8. 20. Putting It Together
      1. Missing Pieces
      2. Planning the Build
        1. The Development Build
        2. The Integration Build
        3. The Release Build
      3. Using a CI System
        1. Jenkins
        2. Other CI Systems
  6. A. JavaScript Style Guide
    1. Indentation
    2. Line Length
    3. Primitive Literals
    4. Operator Spacing
    5. Parentheses Spacing
    6. Object Literals
    7. Comments
      1. Single-Line Comments
      2. Multiline Comments
      3. Comment Annotations
    8. Variable Declarations
    9. Function Declarations
    10. Naming
    11. Strict Mode
    12. Assignments
    13. Equality Operators
    14. Ternary Operator
    15. Statements
      1. Simple Statements
      2. return Statement
      3. Compound Statements
      4. if Statement
      5. for Statement
      6. while Statement
      7. do Statement
      8. switch Statement
      9. try Statement
    16. White Space
    17. Things to Avoid
  7. B. JavaScript Tools
    1. Build Tools
    2. Documentation Generators
    3. Linting Tools
    4. Minification Tools
    5. Testing Tools
  8. Index
  9. About the Author
  10. Colophon
  11. Copyright

Product information

  • Title: Maintainable JavaScript
  • Author(s): Nicholas C. Zakas
  • Release date: May 2012
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449327682