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 writing a client-side, server-side, or desktop application with JavaScript. 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 Copyright......Page 6 Table of Contents......Page 9 Conventions Used in This Book......Page 15 Safari® Books Online......Page 16 Acknowledgments......Page 17 Reading......Page 18 Patterns......Page 19 Object-Oriented......Page 21 Prototypes......Page 22 ECMAScript 5......Page 23 The Console......Page 24 Writing Maintainable Code......Page 27 Minimizing Globals......Page 28 The Problem with Globals......Page 29 Side Effects When Forgetting var......Page 30 Single var Pattern......Page 31 Hoisting: A Problem with Scattered vars......Page 32 for Loops......Page 33 for-in Loops......Page 35 (Not) Augmenting Built-in Prototypes......Page 37 switch Pattern......Page 38 Avoiding eval()......Page 39 Coding Conventions......Page 41 Curly Braces......Page 42 Opening Brace Location......Page 43 White Space......Page 44 Separating Words......Page 46 Other Naming Patterns......Page 47 Writing API Docs......Page 48 YUIDoc Example......Page 49 Writing to Be Read......Page 52 Peer Reviews......Page 53 Minify...In Production......Page 54 Summary......Page 55 Object Literal......Page 57 The Object Literal Syntax......Page 58 Object Constructor Catch......Page 59 Custom Constructor Functions......Page 60 Constructor’s Return Values......Page 61 Patterns for Enforcing new......Page 62 Using that......Page 63 Array Literal......Page 64 Array Constructor Curiousness......Page 65 Check for Array-ness......Page 66 Working with JSON......Page 67 Regular Expression Literal......Page 68 Regular Expression Literal Syntax......Page 69 Primitive Wrappers......Page 70 Error Objects......Page 71 Summary......Page 72 Background......Page 75 Disambiguation of Terminology......Page 76 Declarations Versus Expressions: Names and Hoisting......Page 77 Function’s name Property......Page 78 Function Hoisting......Page 79 Callback Pattern......Page 80 A Callback Example......Page 81 Callbacks and Scope......Page 82 Timeouts......Page 84 Returning Functions......Page 85 Self-Defining Functions......Page 86 Immediate Functions......Page 87 Parameters of an Immediate Function......Page 88 Returned Values from Immediate Functions......Page 89 Benefits and Usage......Page 90 Immediate Object Initialization......Page 91 Init-Time Branching......Page 92 Function Properties—A Memoization Pattern......Page 94 Configuration Objects......Page 95 Function Application......Page 97 Partial Application......Page 98 Currying......Page 99 When to Use Currying......Page 101 Summary......Page 102 Namespace Pattern......Page 105 General Purpose Namespace Function......Page 107 Declaring Dependencies......Page 108 Private Members......Page 110 Privacy Failures......Page 111 Object Literals and Privacy......Page 112 Prototypes and Privacy......Page 113 Revealing Private Functions As Public Methods......Page 114 Module Pattern......Page 115 Revealing Module Pattern......Page 117 Modules That Create Constructors......Page 118 A Global Constructor......Page 119 Adding Modules......Page 121 Implementing the Constructor......Page 122 Public Static Members......Page 123 Private Static Members......Page 125 Object Constants......Page 127 Chaining Pattern......Page 128 Pros and Cons of the Chaining Pattern......Page 129 method() Method......Page 130 Summary......Page 131 Classical Versus Modern Inheritance Patterns......Page 133 Expected Outcome When Using Classical Inheritance......Page 134 Following the Prototype Chain......Page 135 Drawbacks When Using Pattern #1......Page 137 Classical Pattern #2—Rent-a-Constructor......Page 138 The Prototype Chain......Page 139 Multiple Inheritance by Borrowing Constructors......Page 140 Classical Pattern #3—Rent and Set Prototype......Page 141 Classical Pattern #4—Share the Prototype......Page 142 Classical Pattern #5—A Temporary Constructor......Page 143 Storing the Superclass......Page 144 Resetting the Constructor Pointer......Page 145 Klass......Page 146 Prototypal Inheritance......Page 148 Addition to ECMAScript 5......Page 150 Inheritance by Copying Properties......Page 151 Mix-ins......Page 153 Borrowing Methods......Page 154 Borrow and Bind......Page 155 Function.prototype.bind()......Page 156 Summary......Page 157 Singleton......Page 159 Using new......Page 160 Instance in a Static Property......Page 161 Instance in a Closure......Page 162 Factory......Page 164 Built-in Object Factory......Page 166 Iterator......Page 167 Implementation......Page 169 Implementation Using a List......Page 172 Strategy......Page 173 Data Validation Example......Page 174 Façade......Page 176 Proxy......Page 177 A video expando......Page 178 Without a proxy......Page 179 Event handlers......Page 180 videos object......Page 181 http object......Page 182 Enter the proxy......Page 183 Mediator......Page 185 Mediator Example......Page 186 Example #1: Magazine Subscriptions......Page 189 Example #2: The Keypress Game......Page 193 Summary......Page 196 Separation of Concerns......Page 199 DOM Access......Page 201 DOM Manipulation......Page 202 Events......Page 203 Event Handling......Page 204 Event Delegation......Page 206 setTimeout()......Page 207 Remote Scripting......Page 208 XMLHttpRequest......Page 209 JSONP......Page 210 JSONP example: Tic-tac-toe......Page 211 Frames and Image Beacons......Page 213 Combining Scripts......Page 214 Using a CDN......Page 215 Loading Strategies......Page 216 The Place of the Element......Page 217 HTTP Chunking......Page 218 Dynamic Element for Nonblocking Downloads......Page 219 Appending the element......Page 220 Loading on Demand......Page 221 Preloading JavaScript......Page 223 Summary......Page 224 Index......Page 227 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 moreLearn why literal notation patterns are simpler alternatives to constructor functionsDiscover different ways to define a function in JavaScriptCreate objects that go beyond the basic patterns of using object literals and constructor functionsLearn the options available for code reuse and inheritance in JavaScriptStudy sample JavaScript approaches to common design patterns such as Singleton, Factory, Decorator, and moreExamine patterns that apply specifically to the client-side browser environment 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{u2014}whether you're using JavaScript to write a client-side, server-side, or desktop application. Written by JavaScript expert Stoyan Stefanov{u2014}Senior Yahoo! Technical and architect of YSlow 2.0, the web page performance optimization tool{u2014}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 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