Mastering JavaScript Design Patterns
eBook - ePub

Mastering JavaScript Design Patterns

Simon Timms

Share book
  1. 290 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering JavaScript Design Patterns

Simon Timms

Book details
Book preview
Table of contents
Citations

About This Book

Applying design patterns to JavaScript code creates more reliable and maintainable code. In this book, you will explore different design patterns and learn where and how to implement them with the help of detailed examples.

In the first part of the book, we start off with an introduction to design patterns, and then move on to creating classical structures that are used to organize code. Next, we look at the creational, structural, and behavioral patterns. The second part of the book dives into patterns used for functional programming, model view patterns, patterns to build web applications, and messaging patterns. A number of very interesting advanced JavaScript patterns such as dependency injection and live postprocessing are also covered.

By the end of this book, you will learn to identify places where a pattern would improve code readability and maintainability.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Mastering JavaScript Design Patterns an online PDF/ePUB?
Yes, you can access Mastering JavaScript Design Patterns by Simon Timms in PDF and/or ePUB format, as well as other popular books in Informatique & Programmation Web. We have over one million books available in our catalogue for you to explore.

Information

Year
2014
ISBN
9781783987986
Edition
1

Mastering JavaScript Design Patterns


Table of Contents

Mastering JavaScript Design Patterns
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
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
1. 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?
Antipatterns
Summary
I. Classical Design Patterns
2. Organizing Code
Chunks of code
What's the matter with global scope anyway?
Objects in JavaScript
Build me a prototype
Inheritance
Modules
ECMAScript 6 classes and modules
Best practices and troubleshooting
Summary
3. Creational Patterns
Abstract Factory
Implementation
Builder
Implementation
Factory Method
Implementation
Singleton
Implementation
Disadvantages
Prototype
Implementation
Hints and tips
Summary
4. Structural Patterns
Adapter
Implementation
Bridge
Implementation
Composite
An example
Implementation
Decorator
Implementation
Façade
Implementation
Flyweight
Implementation
Proxy
Implementation
Hints and tips
Summary
5. Behavioral Patterns
Chain of responsibility
Implementation
Command
The command message
The invoker
The receiver
Interpreter
An example
Implementation
Iterator
Implementation
ECMAScript 6 iterators
Mediator
Implementation
Memento
Implementation
Observer
Implementation
State
Implementation
Strategy
Implementation
Template method
Implementation
Visitor
Hints and tips
Summary
II. Other Patterns
6. 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
7. Model View Patterns
First, some history
Model View Controller
The MVC code
Model View Presenter
The MVP code
Model View ViewModel
The MVVM code
A better way to transfer changes between the model and the view
Observing view changes
Hints and tips
Summary
8. Web Patterns
Sending JavaScript
Combining files
Minification
Content delivery networks
Plugins
jQuery
d3
Doing two things at once – multithreading
The circuit breaker pattern
Back-off
Degraded application behavior
The promise pattern
Hints and tips
Summary
9. Messaging Patterns
What's a message anyway?
Commands
Events
Request-reply
Publish-subscribe
Fan out and fan in
Dead-letter queues
Message replay
Pipes and filters
Versioning messages
Hints and tips
Summary
10. Patterns for Testing
The testing pyramid
Test in the small with unit tests
Arrange-Act-Assert
Asserts
Fake objects
Test spies
Stub
Mock
Monkey patching
Interacting with the user interface
Browser testing
Faking the DOM
Wrapping the manipulation
Build and test tools
Hints and tips
Summary
11. Advanced Patterns
Dependency injection
Live postprocessing
Aspect-oriented programming
Macros
Hints and tips
Summary
12. ES6 Solutions Today
TypeScript
The class syntax
The module syntax
Arrow functions
Typing
Traceur
Classes
Default parameters
Template literals
Block bindings with let
Async
Conclusion
Hints and tips
Summary
A. Conclusion
Index

Mastering JavaScript Design Patterns

Copyright © 2014 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
Production reference: 1151114
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-798-6
www.packtpub.com

Credits

Author
Simon Timms
Reviewers
Amrita Chaturvedi
Philippe Renevier Gonin
Pedro Miguel Barros Morgado
Mani Nilchiani
Commissioning Editor
Kunal Parikh
Acquisition Editor
Meeta Rajani
Content Development Editor
Sweny M. Sukumaran
Technical Editor
Siddhi Rane
Copy Editor
Laxmi Subramanian
Project Coordinator
Rashi Khivansara
Proofreaders
Simran Bhogal
Lawrence A. Herman
Elinor Perry-Smith
Indexer
Hemangini Bari
Graphics
Sheetal Aute
Ronak Dhruv
Valentina D'silva
Disha Haria
Abhinash Sahu
Production Coordinator
Nitesh Thakur
Cover Work
Nitesh Thakur

About the Author

Simon Timms is a developer who loves to write code. He writes in a variety of languages using a number of tools. For the most part, he develops web applications with .NET backends. He is very interested in visualizations and cloud computing. A background in build and system administration keeps him on the straight and narrow when it comes to DevOps.
He is the author of Social Data Visualization with HTML5 and JavaScript, Packt Publishing. He blogs at http://blog.simontimms.com/ and is also a frequent contributor to the Canadian Developer Connection, where his latest series explores evolving ASP.NET applications.
He is the President of the Calgary .NET User Group and a member of half a dozen others. He speaks on a variety of topics from DevOps to how the telephone system works.
He works as a web developer for Pacesetter Directional Drilling, the friendliest performance drilling company around.

About the Reviewers

Amrita Chaturvedi is a PhD researcher in the Department of Computer Science and Engineering at Indian Institute of Technology, Kanpur, Uttar Pradesh, India (http://www.cse.iitk.ac.in/users/amrita/index.htm). She was schooled (kindergarten to intermediate) at City Montessori School, Aliganj, Lucknow, Uttar Pradesh, India. She received a Bachelor of Technology degree in Information Technology from Institute of Engineering and Technology, Lucknow, Uttar Pradesh, India and a Master of Technology degree in Information Technology (with a specialization in Software Engineering) from Indian Institute of Information Technology, Allahabad (Deemed University), Uttar Pradesh, India. She has worked in Nucleus Software Exports Ltd., Noida, Uttar Pradesh, India as a software engineer. She was also employed as a faculty in Institute of Engineering and Technology, Lucknow, Uttar Pradesh, India. She has worked in user interface design as a senior project associate at Indian Institute of Technology, Kanpur, Uttar Pradesh, India. She was selected as the first female PhD student from Asia under EURECA (European Research and Educational Collaboration with Asia) project 2009 to conduct research at VU University Amsterdam, the Netherlands. Her areas of specialization are software engineering, software architecture, software design patterns, and ontologies. Her research interests include software architecture and design, ontologies-based software engineering, service-oriented and model-driven architecture, semantic web, Internet technologies, and mobile agents.
She has given several talks and seminars as well as conference welcome/key notes at international conferences. She has also earned various awards such as best paper award for her research paper in an international conference, ACM SIGAPP award, and has also been a Physics Olympiad topper. She has traveled several European, North American, African, and Asian countries for educational/conference/research purposes. She has teaching as well as research exper...

Table of contents