JavaScript Patterns JumpStart Guide (Clean up your JavaScript Code)
By Dan Wahlin
4.5/5
()
About this ebook
Are you writing a lot of JavaScript code and finding that it's growing into an unmanageable mess? In this concise and focused jump start guide you'll learn how to fix common problems such as function spaghetti code and pollution of the global scope, and gain the skills necessary to write clean JavaScript code. By applying patterns, JavaScript code can be made more modular and easier to work with and that's really the overall purpose of this guide. You'll be provided with focused tips and sample code that will help you write maintainable, re-usable and consistent JavaScript.
Key concepts covered in this guide include:
• Function spaghetti code and why it's bad
• The role of closures
• Namespaces and global scope
• Different techniques for defining variables
• Working with JavaScript's prototype property
• The Prototype Pattern
• The Revealing Module Pattern
• The Revealing Prototype Pattern
The sample code shown is also available as a download.
Dan Wahlin
Dan Wahlin founded The Wahlin Group (http://www.TheWahlinGroup.com) which provides consulting and training services on Web technologies such as JavaScript, jQuery, AngularJS, SPAs, HTML5, ASP.NET and SharePoint. He's also one of the top authors at Pluralsight.com and has published several courses covering a range of technologies. Dan is a Microsoft Regional Director and has been awarded Microsoft's MVP award multiple times for ASP.NET, Connected Systems and Silverlight. Dan is on the INETA Speaker's Bureau, speaks at conferences and user groups around the world and has written several books on Web technologies. Dan blogs at http://weblogs.asp.net/dwahlin and writes regular columns for various technical magazines. Follow Dan on Twitter @DanWahlin.
Related to JavaScript Patterns JumpStart Guide (Clean up your JavaScript Code)
Related ebooks
JavaScript Unlocked Rating: 5 out of 5 stars5/5Mastering JavaScript Rating: 4 out of 5 stars4/5Mastering JavaScript Design Patterns - Second Edition Rating: 5 out of 5 stars5/550 Recipes for Programming Node.js Rating: 3 out of 5 stars3/5JavaScript: Advanced Guide to Programming Code with JavaScript Rating: 0 out of 5 stars0 ratingsJavaScript: Tips and Tricks to Programming Code with Javascript Rating: 0 out of 5 stars0 ratingsA Pocket Guide to CSS Animations Rating: 5 out of 5 stars5/5JavaScript: Tips and Tricks to Programming Code with Javascript: JavaScript Computer Programming, #2 Rating: 0 out of 5 stars0 ratingsJavaScript: Advanced Guide to Programming Code with Javascript: JavaScript Computer Programming, #4 Rating: 0 out of 5 stars0 ratingsReact.js Essentials Rating: 4 out of 5 stars4/5Understanding JavaScript Promises Rating: 0 out of 5 stars0 ratingsJavaScript: Beginner's Guide to Programming Code with JavaScript Rating: 5 out of 5 stars5/5Javascript For Beginners: Your Guide For Learning Javascript Programming in 24 Hours Rating: 3 out of 5 stars3/5JavaScript for the Business Developer Rating: 3 out of 5 stars3/5Mastering JavaScript: The Complete Guide to JavaScript Mastery Rating: 5 out of 5 stars5/5JavaScript: Beginner's Guide to Programming Code with JavaScript: JavaScript Computer Programming Rating: 0 out of 5 stars0 ratingsReact: Building Modern Web Applications Rating: 5 out of 5 stars5/5100 Recipes for Programming Java Rating: 5 out of 5 stars5/5Quick JavaScript Learning In Just 3 Days: Fast-Track Learning Course Rating: 0 out of 5 stars0 ratingsLearn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5React Components Rating: 0 out of 5 stars0 ratingsLearn Javascript In 1 Hour Rating: 0 out of 5 stars0 ratingsAngular JS for Beginners: Your Guide to Easily Learn Angular JS In 7 Days Rating: 2 out of 5 stars2/5Javascript Concepts: 1St Edition Rating: 0 out of 5 stars0 ratingsJavaScript for .NET Developers Rating: 0 out of 5 stars0 ratings10 Lessons in Front-end Rating: 2 out of 5 stars2/5Jump Start Vue.js Rating: 4 out of 5 stars4/5Django Admin Cookbook Rating: 0 out of 5 stars0 ratingsObject-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications, and libraries Rating: 3 out of 5 stars3/5
Programming For You
Game Development with Unreal Engine 5: Learn the Basics of Game Development in Unreal Engine 5 (English Edition) Rating: 0 out of 5 stars0 ratingsPython: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5The Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsLearn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsCoding All-in-One For Dummies Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Beginning Programming with Python For Dummies Rating: 3 out of 5 stars3/5Web Designer's Idea Book, Volume 4: Inspiration from the Best Web Design Trends, Themes and Styles Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5
Reviews for JavaScript Patterns JumpStart Guide (Clean up your JavaScript Code)
3 ratings0 reviews
Book preview
JavaScript Patterns JumpStart Guide (Clean up your JavaScript Code) - Dan Wahlin
About the Author
Welcome to the JavaScript Patterns JumpStart Guide! My name is Dan Wahlin and I'll be walking you through a lot of great information that will change how you write JavaScript and result in more maintainable applications. I've been building JavaScript applications since the 90s and love writing about and working with the language. If you're interested in keeping up with all of the great enhancements in the JavaScript language as well as various frameworks, check out my blog or follow me on Twitter.
rss Blog
http://weblogs.asp.net/dwahlin
Twitter Twitter
@DanWahlin
Table of Contents
About the Author
Sample Code
Introduction
Function Spaghetti Code
What are Closures?
Namespaces and Global Scope
Defining Variables
The Prototype Pattern
Revealing Module Pattern
Creating Multiple Objects with the Revealing Module Pattern
Revealing Prototype Pattern
Working with the JavaScript this Keyword
Using the JavaScript prototype Property to Structure and Extend Code
Getting Started with JavaScript Prototyping
Overriding with Prototype
Conclusion
Sample Code
A lot of effort went into formatting the code shown in this book to make it easy to read and follow. However, different devices display text in different ways so I've made the book's sample code available at http://tinyurl.com/StructuringJSCode so that you can view it in your favorite editor as well.
Introduction
JavaScript has come a long way since the mid-90s when I first started working with it in Netscape Navigator. Back in the day I thought JavaScript was painful to use but over the years I've learned to love it and appreciate what it offers as a language. JavaScript is quite flexible and can perform a wide variety of tasks on both the client-side and server-side. In fact, I used to prefer it to VBScript on the server-side when writing classic ASP applications, and today we have server-side frameworks such as Node.js that are JavaScript based. With the rise of HTML5 and new features such as the Canvas API and SVG, JavaScript is more important than ever when building applications. As applications use more JavaScript, it's important the code is structured in a way that's easy to work with and maintain.
Although JavaScript isn't designed with the concept of classes or object-oriented programming in mind as with C# or Java (at least not until ECMAScript 6 comes out and is supported by all of the browsers), with a little work you can achieve similar results. In this guide I'll discuss a few popular techniques, strategies, and patterns for structuring JavaScript to encapsulate functionality much like classes do, hide private members, and provide a better overall re-use strategy and maintenance story in applications. These techniques work well across all of the major browser out there.
The patterns that will be discussed include the following:
Prototype Pattern
Revealing Module Pattern
Revealing Prototype Pattern
Are there more patterns? Certainly! However, the goal of this jumpstart guide to focus on