Mastering JavaScript Design Patterns Second Edition Table of Contents Mastering JavaScript Design Patterns Second Edition Credits About the Author About the Reviewer www.PacktPub.com eBooks, discount offers, and more Why subscribe? Preface What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Errata Piracy Questions Designing for Fun and Profit The road to JavaScript The early days A pause The way of GMail JavaScript everywhere What is a design pattern? Anti-patterns Summary I Classical Design Patterns Organizing Code Chunks of code What's the matter with global scope anyway? Objects in JavaScript Build me a prototype Inheritance Modules ECMAScript 2015 classes and modules Best practices and troubleshooting Summary Creational Patterns Abstract factory Implementation Builder Implementation Factory method Implementation Singleton Implementation Disadvantages Prototype Implementation Tips and tricks Summary Structural Patterns Adapter Implementation Bridge Implementation Composite Example Implementation Decorator Implementation Faỗade Implementation Flyweight Implementation Proxy Implementation Hints and tips Summary Behavioral Patterns Chain of responsibility Implementation Command Command message Invoker Receiver Interpreter Example Implementation Iterator Implementation ECMAScript 2015 iterators Mediator Implementation Memento Implementation Observer Implementation State Implementation Strategy Implementation Template method Implementation Visitor Hints and tips Summary II Other Patterns Functional Programming Functional functions are side-effect-free Function passing Implementation Filters and pipes Implementation Accumulators Implementation Memoization Implementation Immutability Lazy instantiation Implementation Hints and tips Summary Reactive Programming Application state changes Streams Filtering streams Merging streams Streams for multiplexing Hints and tips Summary Application Patterns First, some history Model View Controller MVC code Model View Presenter MVP code Model View ViewModel MVVM code A better way to transfer changes between the model and the view Observing view changes Tips and tricks Summary Web Patterns Sending JavaScript Combining files Minification Content Delivery Networks Plugins jQuery d3 Doing two things at once – multithreading Circuit breaker pattern Back-off Degraded application behavior Promise pattern Hints and tips Summary 10 Messaging Patterns What's a message anyway? Commands Events Request-reply Publish-subscribe Fan out and in Dead letter queues Message replay Pipes and filters Versioning messages Hints and tips Summary 11 Microservices Faỗade Service selector Aggregate services Pipeline Message upgrader Failure patterns Service degradation Message storage Message replay Indempotence of message handling Hints and tips Summary 12 Patterns for Testing The testing pyramid Testing in the small with unit tests Arrange-Act-Assert Assert Fake objects Test spies Stubs Mock Monkey patching Interacting with the user interface Browser testing Faking the DOM Wrapping the manipulation Tips and tricks Summary 13 Advanced Patterns Dependency injection Live post processing Aspect oriented programming Mixins Macros Tips and tricks Summary 14 ECMAScript-2015/2016 Solutions Today TypeScript Decorators Async/Await Typing BabelJS Classes Default parameters Template literals Block bindings with let In production Tips and tricks Summary Index Mastering JavaScript Design Patterns Second Edition Mastering JavaScript Design Patterns Second Edition Copyright © 2016 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information First published: November 2014 Second published: June 2016 Production reference: 1240616 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78588-216-6 www.packtpub.com E ECMAScript / The early days ECMAScript 2015 classes / ECMAScript 2015 classes and modules modules / ECMAScript 2015 classes and modules / Hints and tips envelope / What's a message anyway? European Computer Manufacturers Association (ECMA) / The early days F factory method about / Factory method implementing / Implementation failure patterns about / Failure patterns service degradation / Service degradation message storage / Message storage message replay / Message replay message handling, indempotence / Indempotence of message handling fake objects about / Fake objects Faỗade about / Faỗade faỗade pattern about / Faỗade implementing / Implementation filters and pipes about / Filters and pipes implementing / Implementation flyweight pattern about / Flyweight implementing / Implementation functional functions about / Functional functions are side-effect-free function passing about / Function passing implementing / Implementation G global scope about / What's the matter with global scope anyway? I immutability about / Immutability inheritance about / Inheritance integration testing / Fake objects interpreter pattern about / Interpreter example / Example implementing / Implementation iterator about / Iterator implementing / Implementation ECMAScript 2015 iterators / ECMAScript 2015 iterators J JavaScript about / The road to JavaScript, JavaScript everywhere, Best practices and troubleshooting early days / The early days GMail / The way of GMail Asynchronous JavaScript and XML (AJAX) / The way of GMail objects / Objects in JavaScript sending / Sending JavaScript files, combining / Combining files minification / Minification JavaScript Object Notation (JSON) / The way of GMail jQuery / jQuery L lazy instantiation about / Lazy instantiation implementing / Implementation live post processing about / Live post processing M macros about / Macros mediator about / Mediator implementing / Implementation memento about / Memento originator player / Memento caretaker player / Memento player / Memento implementing / Implementation memoization about / Memoization implementing / Implementation message about / What's a message anyway? commands / Commands events / Events message handling indempotence / Indempotence of message handling messages hints and tips / Hints and tips message storage / Message storage message upgrader about / Message upgrader microservices hints and tips / Hints and tips mixins about / Mixins mock about / Mock Model View Controller (MVC) about / Model View Controller code / MVC code Model View Presenter (MVP) about / Model View Presenter code / MVP code Model View ViewModel (MVVM) about / Model View ViewModel code / MVVM code model and view, changes transfering / A better way to transfer changes between the model and the view view changes, observing / Observing view changes tips and tricks / Tips and tricks modules about / Modules monkey patching / Build me a prototype about / Monkey patching multithreading about / Doing two things at once – multithreading O observer pattern about / Observer implementing / Implementation P Palo Alto Research Center (PARC) / Chunks of code pattern hints and tips / Hints and tips, Hints and tips pipeline about / Pipeline plugins about / Plugins jQuery / jQuery d3 / d3 promise pattern about / Promise pattern prototype building / Build me a prototype about / Prototype implementing / Implementation proxy pattern about / Proxy implementing / Implementation publish-subscribe model about / Publish-subscribe fan out and in / Fan out and in R Reactive Extensions URL / Streams request-reply about / Request-reply S service degradation / Service degradation service selector about / Service selector Single Page Applications (SPAs) / The way of GMail singleton disadvantages / Disadvantages singleton pattern about / Singleton implementing / Implementation state about / State implementing / Implementation strategy about / Strategy implementing / Implementation streams about / Streams filtering / Filtering streams merging / Merging streams for multiplexing / Streams for multiplexing hints and tips / Hints and tips Structured Query Language (SQL) / Interpreter stub about / Stubs T template literals / Template literals template method about / Template method implementing / Implementation testing pyramid about / The testing pyramid test spies about / Test spies TypeScript about / TypeScript decorators / Decorators Async/Await / Async/Await typing / Typing typing / Typing U unit tests used, for testing / Testing in the small with unit tests user interface about / Interacting with the user interface browser testing / Browser testing DOM, faking / Faking the DOM manipulation, wrapping / Wrapping the manipulation tips and tricks / Tips and tricks V visitor pattern about / Visitor .. .Mastering JavaScript Design Patterns Second Edition Table of Contents Mastering JavaScript Design Patterns Second Edition Credits About the Author About... let In production Tips and tricks Summary Index Mastering JavaScript Design Patterns Second Edition Mastering JavaScript Design Patterns Second Edition Copyright © 2016 Packt Publishing All rights... Questions Designing for Fun and Profit The road to JavaScript The early days A pause The way of GMail JavaScript everywhere What is a design pattern? Anti -patterns Summary I Classical Design Patterns