Build Better Applications with Coding and Design Patterns

What's the best approach for developing an application with JavaScript? This book helps you answer that question with numerous JavaScript coding patterns and best practices. If you're an experienced developer looking to solve problems related to objects, functions, inheritance, and other language-specific categories, the abstractions and code templates in this guide are ideal—whether you're using JavaScript to write a client-side, server-side, or desktop application.

Written by JavaScript expert Stoyan Stefanov—Senior Yahoo! Technical and architect of YSlow 2.0, the web page performance optimization tool—JavaScript Patterns includes practical advice for implementing each pattern discussed, along with several hands-on examples. You'll also learn about anti-patterns: common programming approaches that cause more problems than they solve.

  • Explore useful habits for writing high-quality JavaScript code, such as avoiding globals, using single var declarations, and more
  • Learn why literal notation patterns are simpler alternatives to constructor functions
  • Discover different ways to define a function in JavaScript
  • Create objects that go beyond the basic patterns of using object literals and constructor functions
  • Learn the options available for code reuse and inheritance in JavaScript
  • Study sample JavaScript approaches to common design patterns such as Singleton, Factory, Decorator, and more
  • Examine patterns that apply specifically to the client-side browser environment

Stoyan Stefanov

Stoyan Stefanov is a Yahoo! web developer, Zend Certified Engineer, and an author, contributor, and tech reviewer of various O'Reilly books. He speaks regularly about web development topics at conferences and on his blog at Stoyan is the creator of the image optimization tool and architect of Yahoo's performance optimization tool YSlow 2.0.

  1. Chapter 1 Introduction

    1. Patterns

    2. JavaScript: Concepts

    3. ECMAScript 5

    4. JSLint

    5. The Console

  2. Chapter 2 Essentials

    1. Writing Maintainable Code

    2. Minimizing Globals

    3. for Loops

    4. for-in Loops

    5. (Not) Augmenting Built-in Prototypes

    6. switch Pattern

    7. Avoiding Implied Typecasting

    8. Avoiding eval()

    9. Number Conversions with parseInt()

    10. Coding Conventions

    11. Naming Conventions

    12. Writing Comments

    13. Writing API Docs

    14. Writing to Be Read

    15. Peer Reviews

    16. Minify…In Production

    17. Run JSLint

    18. Summary

  3. Chapter 3 Literals and Constructors

    1. Object Literal

    2. Custom Constructor Functions

    3. Patterns for Enforcing new

    4. Array Literal

    5. JSON

    6. Regular Expression Literal

    7. Primitive Wrappers

    8. Error Objects

    9. Summary

  4. Chapter 4 Functions

    1. Background

    2. Callback Pattern

    3. Returning Functions

    4. Self-Defining Functions

    5. Immediate Functions

    6. Immediate Object Initialization

    7. Init-Time Branching

    8. Function Properties—A Memoization Pattern

    9. Configuration Objects

    10. Curry

    11. Summary

  5. Chapter 5 Object Creation Patterns

    1. Namespace Pattern

    2. Declaring Dependencies

    3. Private Properties and Methods

    4. Module Pattern

    5. Sandbox Pattern

    6. Static Members

    7. Object Constants

    8. Chaining Pattern

    9. method() Method

    10. Summary

  6. Chapter 6 Code Reuse Patterns

    1. Classical Versus Modern Inheritance Patterns

    2. Expected Outcome When Using Classical Inheritance

    3. Classical Pattern #1—The Default Pattern

    4. Classical Pattern #2—Rent-a-Constructor

    5. Classical Pattern #3—Rent and Set Prototype

    6. Classical Pattern #4—Share the Prototype

    7. Classical Pattern #5—A Temporary Constructor

    8. Klass

    9. Prototypal Inheritance

    10. Inheritance by Copying Properties

    11. Mix-ins

    12. Borrowing Methods

    13. Summary

  7. Chapter 7 Design Patterns

    1. Singleton

    2. Factory

    3. Iterator

    4. Decorator

    5. Strategy

    6. Façade

    7. Proxy

    8. Mediator

    9. Observer

    10. Summary

  8. Chapter 8 DOM and Browser Patterns

    1. Separation of Concerns

    2. DOM Scripting

    3. Events

    4. Long-Running Scripts

    5. Remote Scripting

    6. Deploying JavaScript

    7. Loading Strategies

    8. Summary

  9. Colophon