Pro JavaScript™ Design Patterns

Book description

As a web developer, you'll already know that JavaScript is a powerful language, allowing you to add an impressive array of dynamic functionality to otherwise static web sites. But there is more power waiting to be unlocked—JavaScript is capable of full object–oriented capabilities, and by applying object-oriented principles, best practices, and design patterns to your code, you can make it more powerful, more efficient, and easier to work with alone or as part of a team.

With Pro JavaScript Design Patterns, you'll start with the basics of object–oriented programming in JavaScript applicable to design patterns, including making JavaScript more expressive, inheritance, encapsulation, information hiding, and more. With that covered, you can kick–start your JavaScript development in the second part of the book, where you'll find detail on how to implement and take advantage of several design patterns in JavaScript, including composites, decorators, façades, adapters, and many more.

Each chapter is packed with real–world examples of how the design patterns are best used and expert advice on writing better code, as well as what to watch out for. Along the way you'll discover how to create your own libraries and APIs for even more efficient coding.

  • Master the basics of object–oriented programming in JavaScript, as they apply to design patterns

  • Apply design patterns to your kick–start your JavaScript development

  • Work through several real–world examples

Table of contents

  1. Title Page
  2. Dedication
  3. Contents at a Glance
  4. Contents
  5. About the Authors
  6. About the Technical Reviewer
  7. Acknowledgments
  8. Introduction
    1. Who This Book Is For
    2. How This Book Is Structured
    3. Prerequisites
    4. Downloading the Code
    5. Contacting the Authors
  9. PART 1: Object-Oriented JavaScript
    1. CHAPTER 1: Expressive JavaScript
      1. The Flexibility of JavaScript
      2. A Loosely Typed Language
      3. Functions As First-Class Objects
      4. The Mutability of Objects
      5. Inheritance
      6. Design Patterns in JavaScript
      7. Summary
    2. CHAPTER 2: Interfaces
      1. What Is an Interface?
      2. How Other Object-Oriented Languages Handle Interfaces
      3. Emulating an Interface in JavaScript
      4. The Interface Implementation for This Book
      5. The Interface Class
      6. Patterns That Rely on the Interface
      7. Summary
    3. CHAPTER 3: Encapsulation and Information Hiding
      1. The Information Hiding Principle
      2. Basic Patterns
      3. More Advanced Patterns
      4. Benefits of Using Encapsulation
      5. Drawbacks to Using Encapsulation
      6. Summary
    4. CHAPTER 4: Inheritance
      1. Why Do You Need Inheritance?
      2. Classical Inheritance
      3. Prototypal Inheritance
      4. Comparing Classical and Prototypal Inheritance
      5. Inheritance and Encapsulation
      6. Mixin Classes
      7. Example: Edit-in-Place
      8. When Should Inheritance Be Used?
      9. Summary
    5. CHAPTER 5: The Singleton Pattern
      1. The Basic Structure of the Singleton
      2. Namespacing
      3. A Singleton As a Wrapper for Page-Specific Code
      4. A Singleton with Private Members
      5. Lazy Instantiation
      6. Branching
      7. Example: Creating XHR Objects with Branching
      8. When Should the Singleton Pattern Be Used?
      9. Benefits of the Singleton Pattern
      10. Drawbacks of the Singleton Pattern
      11. Summary
    6. CHAPTER 6: Chaining
      1. The Structure of a Chain
      2. Building a Chainable JavaScript Library
      3. Using Callbacks to Retrieve Data from Chained Methods
      4. Summary
  10. PART 2: Design Patterns
    1. CHAPTER 7: The Factory Pattern
      1. The Simple Factory
      2. The Factory Pattern
      3. When Should the Factory Pattern Be Used?
      4. Example: XHR Factory
      5. Example: RSS Reader
      6. Benefits of the Factory Pattern
      7. Drawbacks of the Factory Pattern
      8. Summary
    2. CHAPTER 8: The Bridge Pattern
      1. Example: Event Listeners
      2. Other Examples of Bridges
      3. Bridging Multiple Classes Together
      4. Example: Building an XHR Connection Queue
      5. When Should the Bridge Pattern Be Used?
      6. Benefits of the Bridge Pattern
      7. Drawbacks of the Bridge Pattern
      8. Summary
    3. CHAPTER 9: The Composite Pattern
      1. The Structure of the Composite
      2. Using the Composite Pattern
      3. Example: Form Validation
      4. Example: Image Gallery
      5. Benefits of the Composite Pattern
      6. Drawbacks of the Composite Pattern
      7. Summary
    4. CHAPTER 10: The Facade Pattern
      1. Some Facade Functions You Probably Already Know About
      2. JavaScript Libraries As Facades
      3. Facades As Convenient Methods
      4. Example: Setting Styles on HTML Elements
      5. Example: Creating an Event Utility
      6. General Steps for Implementing the Facade Pattern
      7. When Should the Facade Pattern Be Used?
      8. Benefits of the Facade Pattern
      9. Drawbacks of the Facade Pattern
      10. Summary
    5. CHAPTER 11: The Adapter Pattern
      1. Characteristics of an Adapter
      2. Adapting Existing Implementations
      3. Example: Adapting One Library to Another
      4. Example: Adapting an Email API
      5. When Should the Adapter Pattern Be Used?
      6. Benefits of the Adapter Pattern
      7. Drawbacks of the Adapter Pattern
      8. Summary
    6. CHAPTER 12: The Decorator Pattern
      1. The Structure of the Decorator
      2. In What Ways Can a Decorator Modify Its Component?
      3. The Role of the Factory
      4. Function Decorators
      5. When Should the Decorator Pattern Be Used?
      6. Example: Method Profiler
      7. Benefits of the Decorator Pattern
      8. Drawbacks of the Decorator Pattern
      9. Summary
    7. CHAPTER 13: The Flyweight Pattern
      1. The Structure of the Flyweight
      2. Example: Car Registrations
      3. Managing Extrinsic State
      4. Example: Web Calendar
      5. Example: Tooltip Objects
      6. Storing Instances for Later Reuse
      7. When Should the Flyweight Pattern Be Used?
      8. General Steps for Implementing the Flyweight Pattern
      9. Benefits of the Flyweight Pattern
      10. Drawbacks of the Flyweight Pattern
      11. Summary
    8. CHAPTER 14: The Proxy Pattern
      1. The Structure of the Proxy
      2. When Should the Proxy Be Used?
      3. Example: Page Statistics
      4. General Pattern for Wrapping a Web Service
      5. Example: Directory Lookup
      6. General Pattern for Creating a Virtual Proxy
      7. Benefits of the Proxy Pattern
      8. Drawbacks of the Proxy Pattern
      9. Summary
    9. CHAPTER 15: The Observer Pattern
      1. Example: Newspaper Delivery
      2. Building an Observer API
      3. Observers in Real Life
      4. Example: Animation
      5. Event Listeners Are Also Observers
      6. When Should the Observer Pattern Be Used?
      7. Benefits of the Observer Pattern
      8. Drawbacks of the Observer Pattern
      9. Summary
    10. CHAPTER 16: The Command Pattern
      1. The Structure of the Command
      2. Types of Command Objects
      3. Example: Menu Items
      4. Example: Undo and Logging
      5. When to Use the Command Pattern
      6. Benefits of the Command Pattern
      7. Drawbacks of the Command Pattern
      8. Summary
    11. CHAPTER 17: The Chain of Responsibility Pattern
      1. The Structure of the Chain of Responsibility
      2. Passing on Requests
      3. Implementing a Chain of Responsibility in an Existing Hierarchy
      4. Event Delegation
      5. When Should the Chain of Responsibility Pattern Be Used?
      6. Example: Image Gallery Revisited
      7. Benefits of the Chain of Responsibility Pattern
      8. Drawbacks of the Chain of Responsibility Pattern
      9. Summary
  11. Index

Product information

  • Title: Pro JavaScript™ Design Patterns
  • Author(s):
  • Release date: December 2007
  • Publisher(s): Apress
  • ISBN: 9781590599082