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
Mastering JavaScript Rating: 4 out of 5 stars4/5JavaScript: Advanced Guide to Programming Code with JavaScript Rating: 0 out of 5 stars0 ratingsJavaScript Unlocked Rating: 5 out of 5 stars5/5JavaScript Enlightenment Rating: 4 out of 5 stars4/5Clean Code in JavaScript: Develop reliable, maintainable, and robust JavaScript Rating: 5 out of 5 stars5/5Learning JavaScript Data Structures and Algorithms Rating: 5 out of 5 stars5/5Mastering JavaScript Design Patterns - Second Edition Rating: 5 out of 5 stars5/5Mastering JavaScript: The Complete Guide to JavaScript Mastery Rating: 5 out of 5 stars5/5Learning jQuery 3 - Fifth Edition Rating: 0 out of 5 stars0 ratingsObject-Oriented JavaScript - Third Edition Rating: 4 out of 5 stars4/5JavaScript: Tips and Tricks to Programming Code with Javascript: JavaScript Computer Programming, #2 Rating: 0 out of 5 stars0 ratingsObject-Oriented JavaScript Rating: 4 out of 5 stars4/5Node.js Web Development - Third Edition Rating: 2 out of 5 stars2/5JavaScript: Tips and Tricks to Programming Code with Javascript Rating: 0 out of 5 stars0 ratingsJavaScript: Beginner's Guide to Programming Code with JavaScript: JavaScript Computer Programming Rating: 0 out of 5 stars0 ratingsJavaScript: Beginner's Guide to Programming Code with JavaScript Rating: 5 out of 5 stars5/5The Joy of JavaScript Rating: 0 out of 5 stars0 ratingsJavaScript: Best Practices to Programming Code with JavaScript Rating: 0 out of 5 stars0 ratingsJavaScript: Best Practice Rating: 0 out of 5 stars0 ratingsJavaScript Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsJavaScript for Gurus: Use JavaScript programming features, techniques and modules to solve everyday problems Rating: 0 out of 5 stars0 ratingsNode.js By Example Rating: 2 out of 5 stars2/5JavaScript Projects for Kids Rating: 0 out of 5 stars0 ratingsJavaScript: Best Practices to Programming Code with JavaScript: JavaScript Computer Programming, #3 Rating: 0 out of 5 stars0 ratingsLearning JavaScript Data Structures and Algorithms - Second Edition Rating: 0 out of 5 stars0 ratingsHow JavaScript Works Rating: 0 out of 5 stars0 ratings120 Advanced JavaScript Interview Questions Rating: 0 out of 5 stars0 ratingsJava 9 Data Structures and Algorithms Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python: 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/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsSQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsHTML in 30 Pages Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Python Machine Learning By Example 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/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Coding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratingsLearn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5A Slackers Guide to Coding with Python: Ultimate Beginners Guide to Learning Python Quick Rating: 0 out of 5 stars0 ratingsJavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/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