JavaScript Patterns. Stoyan Stefanov. O'REILLY' Beijing Cambridge Farnham Koln Sebastopol Tokyo



Similar documents
JavaScript Patterns Stoyan Stefanov

Art of Code Front-end Web Development Training Program

Learning Web App Development

Ajax Development with ASP.NET 2.0

Programming Flash Communication Server

An introduction to creating Web 2.0 applications in Rational Application Developer Version 8.0

Rails Cookbook. Rob Orsini. O'REILLY 8 Beijing Cambridge Farnham Koln Paris Sebastopol Taipei Tokyo

JAVA IN A NUTSHELL O'REILLY. David Flanagan. Fifth Edition. Beijing Cambridge Farnham Köln Sebastopol Tokyo

Enterprise JavaBeans 3.1

NETWORK SECURITY HACKS

C#5.0 IN A NUTSHELL. Joseph O'REILLY. Albahari and Ben Albahari. Fifth Edition. Tokyo. Sebastopol. Beijing. Cambridge. Koln.

The C Programming Language course syllabus associate level

Windows PowerShell Cookbook

Table of Contents. Adding Build Targets to the SDK 8 The Android Developer Tools (ADT) Plug-in for Eclipse 9

Programming in HTML5 with JavaScript and CSS3

Learning ios Programming

Oracle PL/SQL Programming

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

1/23/2012. Rich Internet Applications. The importance of JavaScript. Why learn JavaScript? Many choices open to the developer for server-side

Preface. Motivation for this Book

Performance Testing for Ajax Applications

Fundamentals of Java Programming

Schneps, Leila; Colmez, Coralie. Math on Trial : How Numbers Get Used and Abused in the Courtroom. New York, NY, USA: Basic Books, p i.

JAVA ENTERPRISE IN A NUTSHELL. Jim Farley and William Crawford. O'REILLY 4 Beijing Cambridge Farnham Koln Paris Sebastopol Taipei Tokyo.

Programming 3D Applications with HTML5 and WebGL

WINDOWS SERVER HACKS. HLuHB Darmstadt. O'REILLY 5 Beijing Cambridge Farnham Koln Paris Sebastopol Taipei Tokyo

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

MarkLogic Server. Node.js Application Developer s Guide. MarkLogic 8 February, Copyright 2016 MarkLogic Corporation. All rights reserved.

J j enterpririse. Oracle Application Express 3. Develop Native Oracle database-centric web applications quickly and easily with Oracle APEX

Mobile Web Design with HTML5, CSS3, JavaScript and JQuery Mobile Training BSP-2256 Length: 5 days Price: $ 2,895.00

App Inventor Create Your Own Android Apps

NETWORK SECURITY HACKS *

Programming Social Applications

The Software. Audit Guide. ASQ Quality Press. Milwaukee, Wisconsin. John W. Helgeson

Jenkins: The Definitive Guide

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

JAVA. EXAMPLES IN A NUTSHELL. O'REILLY 4 Beijing Cambridge Farnham Koln Paris Sebastopol Taipei Tokyo. Third Edition.

^/ CS> KRIS. JAMSA, PhD, MBA. y» A- JONES & BARTLETT LEARNING

Automating System Administration with Perl

Rich-Internet Anwendungen auf Basis von ColdFusion und Ajax

Contents. Introduction and System Engineering 1. Introduction 2. Software Process and Methodology 16. System Engineering 53

Curriculum Map. Discipline: Computer Science Course: C++

Java Application Developer Certificate Program Competencies

Web Security, Privacy, and Commerce

Hadoop: The Definitive Guide

PYTHON IN A NUTSHELL. O'REILLY Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo. Alex Martelli. Second Edition

Web Security Testing Cookbook*

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Programming with the Microsoft.NET Framework Using Microsoft Visual Studio 2005 (VB)

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Objectif. Participant. Prérequis. Remarque. Programme. C# 3.0 Programming in the.net Framework. 1. Introduction to the.

Cloud Architecture Patterns

MASTERTAG DEVELOPER GUIDE

This course provides students with the knowledge and skills to develop ASP.NET MVC 4 web applications.

Programming and Software Development CTAG Alignments

C# Cookbook. Stephen Teilhet andjay Hilyard. O'REILLY 8 Beijing Cambridge Farnham Köln Paris Sebastopol Taipei Tokyo '"J""'

PROFESSIONAL. Node.js BUILDING JAVASCRIPT-BASED SCALABLE SOFTWARE. Pedro Teixeira WILEY. John Wiley & Sons, Inc.

TypeScript for C# developers. Making JavaScript manageable

Java the UML Way: Integrating Object-Oriented Design and Programming

Course Title: Software Development

Beginning Smartphone Web Development

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

GUI and Web Programming

Web Services for Management Perl Library VMware ESX Server 3.5, VMware ESX Server 3i version 3.5, and VMware VirtualCenter 2.5

AUDIT REPORT EXAMPLE

Implementing and Administering an Enterprise SharePoint Environment

10CS73:Web Programming

Modern Web Development:

Upgrade to Microsoft Web Applications

Practical ASRNET. Web API. Badrinarayanan Lakshmiraghavan. Apress*

Microsoft Windows PowerShell v2 For Administrators

Course Number: IAC-SOFT-WDAD Web Design and Application Development

TypeScript. Language Specification. Version 1.4

How To Write A Nosql Database In Spring Data Project

PARALLEL JAVASCRIPT. Norm Rubin (NVIDIA) Jin Wang (Georgia School of Technology)

Engineering Design. Software. Theory and Practice. Carlos E. Otero. CRC Press. Taylor & Francis Croup. Taylor St Francis Croup, an Informa business

Java (12 Weeks) Introduction to Java Programming Language

Xperia TM. Read about how Xperia TM devices can be administered in a corporate IT environment

How To Test Your Web Site On Wapt On A Pc Or Mac Or Mac (Or Mac) On A Mac Or Ipad Or Ipa (Or Ipa) On Pc Or Ipam (Or Pc Or Pc) On An Ip

AppDev OnDemand Microsoft Development Learning Library

Practical Android Projects Lucas Jordan Pieter Greyling

JavaScript Programming

The HTTP Plug-in. Table of contents

Common Server Setups For Your Web Application - Part II

Professional. SlickEdif. John Hurst IC..T...L. i 1 8 О 7» \ WILEY \ Wiley Publishing, Inc.

Nasuni Management Console Guide

70-487: Developing Windows Azure and Web Services

How-To: Submitting PDF forms to SharePoint from custom websites

Client-side Web Engineering From HTML to AJAX

Website Optimization Tips for Speed

(An) Optimal Drupal 7 Module Configuration for Site Performance JOE PRICE

Client for a Browser-based Application Platform

Windows Vista The Definitive Guide

ECMAScript 3 rd Edition Compact Profile

Java EE Web Development Course Program

NUTECH COMPUTER TRAINING INSTITUTE 1682 E. GUDE DRIVE #102, ROCKVILLE, MD 20850

Website Performance: Kyle Simpson

XML Processing and Web Services. Chapter 17

COURSE SYLLABUS EDG 6931: Designing Integrated Media Environments 2 Educational Technology Program University of Florida

Transcription:

JavaScript Patterns Stoyan Stefanov O'REILLY' Beijing Cambridge Farnham Koln Sebastopol Tokyo

Table of Contents Preface xi 1. Introduction 1 Patterns 1 JavaScript: Concepts 3 Object-Oriented 3 No Classes 4 Prototypes 4 Environment 5 ECMAScript 5 5 JSLint 6 The Console 6 2. Essentials 9 Writing Maintainable Code 9 Minimizing Globals 10 The Problem with Globals 11 Side Effects When Forgetting var 12 Access to the Global Object 13 Single var Pattern 13 Hoisting: A Problem with Scattered vars 14 for Loops 15 for-in Loops 17 (Not) Augmenting Built-in Prototypes 19 switch Pattern 20 Avoiding Implied Typecasting 21 Avoiding eval() 21 Number Conversions with parselnt() 23 Coding Conventions 23 Indentation 24 Curly Braces 24

Opening Brace Location White Space Naming Conventions Capitalizing Constructors Separating Words Other Naming Patterns Writing Comments Writing API Docs YUIDoc Example Writing to Be Read Peer Reviews Minify...In Production Run JSLint Summary 3. Literals and Constructors Object Literal The Object Literal Syntax Objects from a Constructor Object Constructor Catch Custom Constructor Functions Constructor's Return Values Patterns for Enforcing new Naming Convention Using that Self-Invoking Constructor Array Literal Array Literal Syntax Array Constructor Curiousness Check for Array-ness JSON Working with JSON Regular Expression Literal Regular Expression Literal Syntax Primitive Wrappers Error Objects Summary 4. Functions Background Disambiguation of Terminology Declarations Versus Expressions: Names and Hoisting Function's name Property 25 26 28 28 28 29 30 30 31 34 35 36 37 37, 39 39 40 41 41 42 43 44 45 45 46 46 47 47 48 49 49 50 51 52 53 54 57 57 58 59 60 vi I Table of Contents

Function Hoisting 61 Callback Pattern 62 A Callback Example 63 Callbacks and Scope 64 Asynchronous Event Listeners 66 Timeouts 66 Callbacks in Libraries 67 Returning Functions 67 Self-Defining Functions 68 Immediate Functions 69 Parameters of an Immediate Function 70 Returned Values from Immediate Functions 71 Benefits and Usage 72 Immediate Object Initialization 73 Init-Time Branching 74 Function Properties A Memoization Pattern 76 Configuration Objects 77 Curry 79 Function Application 79 Partial Application 80 Currying 81 When to Use Currying 83 Summary 84 5. Object Creation Patterns 87 Namespace Pattern 87 General Purpose Namespace Function 89 Declaring Dependencies 90 Private Properties and Methods 92 Private Members 92 Privileged Methods 93 Privacy Failures 93 Object Literals and Privacy 94 Prototypes and Privacy 95 Revealing Private Functions As Public Methods 96 Module Pattern 97 Revealing Module Pattern 99 Modules That Create Constructors 100 Importing Globals into a Module 101 Sandbox Pattern 101 A Global Constructor 101 Adding Modules 103 Implementing the Constructor 104 Table of Contents I vii

Static Members 105 Public Static Members 105 Private Static Members 107 Object Constants 109 Chaining Pattern 110 Pros and Cons of the Chaining Pattern 111 methodo Method 112 Summary 113 6. Code Reuse Patterns 115 Classical Versus Modern Inheritance Patterns 115 Expected Outcome When Using Classical Inheritance 116 Classical Pattern #1 The Default Pattern 117 Following the Prototype Chain 117 Drawbacks When Using Pattern # 1 119 Classical Pattern #2 Rent-a-Constructor 120 The Prototype Chain 121 Multiple Inheritance by Borrowing Constructors 122 Pros and Cons of the Borrowing Constructor Pattern 123 Classical Pattern #3 Rent and Set Prototype 123 Classical Pattern #4 Share the Prototype 124 Classical Pattern #5 A Temporary Constructor 125 Storing the Superclass 126 Resetting the Constructor Pointer 127 Klass 128 Prototypal Inheritance 130 Discussion 132 Addition to ECMAScript 5 132 Inheritance by Copying Properties 133 Mix-ins 135 Borrowing Methods 136 Example: Borrow from Array 137 Borrow and Bind 137 Function.prototype.bind() 138 Summary 139 7. Design Patterns 141 Singleton 141 Using new 142 Instance in a Static Property 143 Instance in a Closure 144 Factory 146 Built-in Object Factory 148 viii I Table of Contents

Iterator Decorator Usage Implementation Implementation Using a List Strategy Data Validation Example Facade Proxy An Example Proxy As a Cache Mediator Mediator Example Observer Example #1: Magazine Subscriptions Example #2: The Keypress Game Summary 8. DOM and Browser Patterns Separation of Concerns DOM Scripting DOM Access DOM Manipulation Events Event Handling Event Delegation Long-Running Scripts settimeouto Web Workers Remote Scripting XMLHttpRequest JSONP Frames and Image Beacons Deploying JavaScript Combining Scripts Minifying and Compressing Expires Header Using a CDN Loading Strategies The Place of the <script> Element HTTP Chunking Dynamic <script> Element for Nonblocking Downloads Lazy-Loading 149 151 151 151 154 155 156 158 159 160 167 167 168 171 171 175 178 181 181 183 183 184 185 186 188 189 189 190 190 191 192 195 196 196 197 197 197 198 199 200 201 203 Table of Contents 1 ix

Loading on Demand 203 Preloading JavaScript 205 Summary 206 Index 209 Table of Contents