JavaScript Patterns
to Clean Up Your Code
Dan Wahlin
JavaScript Patterns to Cleanup your Code
This presentation is based on an
online training course available at
http://www.pluralsight.com






JavaScript Patterns to Cleanup your Code













JavaScript Patterns to Cleanup your Code






JavaScript Patterns to Cleanup your Code
function myNonClosure() {
var date = new Date();
return date.getMilliseconds();
}
Variable lost after
function returns
function myClosure() {
var date = new Date();
//nested function
return function () {
return date.getMilliseconds();
};
}
Variable stays around
even after function
returns









var calc = {
tempValue: 1,
add: function(x, y){
return x + y;
}
};

var my = my || {};
my.calc = {
tempValue: 1,
add: function(x, y) {
return x + y;
}
};















var Calculator = function(eq) {
this.eqCtl = document.getElementById(eq);
};
Calculator.prototype = {
add: function (x, y) {
var val = x + y;
this.eqCtl.innerHTML = val;
}
};
var calc = new Calculator('eqCtl');
calc.add(2,2);














var calculator = function(eq) {
var eqCtl = document.getElementById(eq),
doAdd = function(x,y) {
var val = x + y;
eqCtl.innerHTML = val;
};
return { add: doAdd }; //Expose public member
}('eqCtl');
calculator.add(2,2);
















var Calculator = function (eq) {
this.eqCtl = document.getElementById(eq);
};
Calculator.prototype = function() {
var doAdd = function (x, y) {
var val = x + y;
this.eqCtl.innerHTML = val;
};
return { add : doAdd };
}();
var calc = new Calculator('eqCtl');
calc.add(2,2);







Sample Code, Book, and Videos
http://tinyurl.com/StructuringJSCode
http://weblogs.asp.net/dwahlin
JavaScript Patterns to Cleanup your Code

More Related Content

PPTX
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
PPT
Ken 20150306 心得分享
PPT
Google Web Toolkit
DOCX
E script-Examples
PDF
React 101
PDF
Testowanie JavaScript
PDF
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
PDF
Human Talks - StencilJS
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
Ken 20150306 心得分享
Google Web Toolkit
E script-Examples
React 101
Testowanie JavaScript
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
Human Talks - StencilJS

What's hot (20)

PPTX
Building complex User Interfaces with Sitecore and React
PDF
“iOS 11 в App in the Air”, Пронин Сергей, App in the Air
PDF
Ngrx slides
PPTX
Event-Driven Systems With MongoDB
PDF
Utilising the data attribute
PPTX
Rapid prototyping of eclipse rcp applications - Eclipsecon Europe 2017
PPTX
SUGCon 2014 Sitecore MVC
PPTX
Apple.combine
PDF
jQuery - Chapter 4 - DOM Handling
PDF
Seeking the truth from mobile analytics
PPTX
From zero to hero with the reactive extensions for java script
PPTX
Advanced JavaScript Concepts
PPTX
Ngrx: Redux in angular
PDF
jQuery - Chapter 5 - Ajax
PPTX
Wix Automation - DIY - Testing BI Events
PPT
9781305078444 ppt ch10
PDF
Flux - new/old approach to building frontend
PPTX
Wix Automation - Core
PPTX
Resiliency & Security_Ballerina Day CMB 2018
Building complex User Interfaces with Sitecore and React
“iOS 11 в App in the Air”, Пронин Сергей, App in the Air
Ngrx slides
Event-Driven Systems With MongoDB
Utilising the data attribute
Rapid prototyping of eclipse rcp applications - Eclipsecon Europe 2017
SUGCon 2014 Sitecore MVC
Apple.combine
jQuery - Chapter 4 - DOM Handling
Seeking the truth from mobile analytics
From zero to hero with the reactive extensions for java script
Advanced JavaScript Concepts
Ngrx: Redux in angular
jQuery - Chapter 5 - Ajax
Wix Automation - DIY - Testing BI Events
9781305078444 ppt ch10
Flux - new/old approach to building frontend
Wix Automation - Core
Resiliency & Security_Ballerina Day CMB 2018
Ad

Viewers also liked (13)

PPTX
AngularJS in 60ish Minutes
PPTX
Building an End-to-End AngularJS Application
PPT
Using jQuery Templates
PPTX
Development Trends - What's New in the World of Web Development
PPTX
Integrating Security Roles into Microsoft Silverlight Applications
PPTX
Basics of angular directive (Part - 1)
PDF
AngularJS Custom Directives
PDF
Custom AngularJS Directives
PPTX
AngularJS custom directive
PDF
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
PPTX
Building AngularJS Custom Directives
PPTX
Getting Started with ASP.NET MVC 3 and Razor
PPTX
Getting Started Building Windows 8 HTML/JavaScript Metro Apps
AngularJS in 60ish Minutes
Building an End-to-End AngularJS Application
Using jQuery Templates
Development Trends - What's New in the World of Web Development
Integrating Security Roles into Microsoft Silverlight Applications
Basics of angular directive (Part - 1)
AngularJS Custom Directives
Custom AngularJS Directives
AngularJS custom directive
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building AngularJS Custom Directives
Getting Started with ASP.NET MVC 3 and Razor
Getting Started Building Windows 8 HTML/JavaScript Metro Apps
Ad

Similar to JavaScript Patterns to Cleanup your Code (20)

PPT
Javascript design patterns
PPT
Java Script Patterns
PPTX
JavaScript (without DOM)
PPT
Douglas Crockford Presentation Goodparts
PDF
Ch 7: Object-Oriented JavaScript
PDF
Future-proofing Your JavaScript Apps (Compact edition)
PPTX
Advanced JavaScript
PPT
Goodparts
PDF
JavaScript Patterns
PDF
Java scriptpatterns
PDF
Java scriptpatterns
PDF
Design patterns in java script, jquery, angularjs
PPT
Wakanday JS201 Best Practices
PPTX
Javascript fundamentals and not
PPTX
JavaScript Proven Practises
PDF
JavaScript: Patterns, Part 1
PDF
java script functions, classes
PPT
Intermediate JavaScript
PDF
The mighty js_function
PPTX
Advanced JavaScript
Javascript design patterns
Java Script Patterns
JavaScript (without DOM)
Douglas Crockford Presentation Goodparts
Ch 7: Object-Oriented JavaScript
Future-proofing Your JavaScript Apps (Compact edition)
Advanced JavaScript
Goodparts
JavaScript Patterns
Java scriptpatterns
Java scriptpatterns
Design patterns in java script, jquery, angularjs
Wakanday JS201 Best Practices
Javascript fundamentals and not
JavaScript Proven Practises
JavaScript: Patterns, Part 1
java script functions, classes
Intermediate JavaScript
The mighty js_function
Advanced JavaScript

Recently uploaded (20)

PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
Architecture types and enterprise applications.pdf
PDF
Comparative analysis of machine learning models for fake news detection in so...
PDF
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PPTX
Modernising the Digital Integration Hub
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PPTX
The various Industrial Revolutions .pptx
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
The influence of sentiment analysis in enhancing early warning system model f...
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Microsoft Excel 365/2024 Beginner's training
Architecture types and enterprise applications.pdf
Comparative analysis of machine learning models for fake news detection in so...
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
OpenACC and Open Hackathons Monthly Highlights July 2025
Modernising the Digital Integration Hub
A contest of sentiment analysis: k-nearest neighbor versus neural network
Convolutional neural network based encoder-decoder for efficient real-time ob...
The various Industrial Revolutions .pptx
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
sbt 2.0: go big (Scala Days 2025 edition)
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
Module 1.ppt Iot fundamentals and Architecture
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Getting started with AI Agents and Multi-Agent Systems
A proposed approach for plagiarism detection in Myanmar Unicode text
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf

JavaScript Patterns to Cleanup your Code

Editor's Notes

  • #6: Discuss what function spaghetti code is (simply listing function after function without a container)
  • #7: Uses standards-based CSS
  • #11: Uses standards-based CSS
  • #12: Uses standards-based CSS
  • #13: Uses standards-based CSS
  • #25: setVal = function (val) {           this.currNumberCtl.innerHTML = val;       },       setEquation = function (val) {           this.eqCtl.innerHTML = val;       },       clearNumbers = function () {           lastNumber = null;           equalsPressed = operatorSet = false;           setVal.call(this, '0');           setEquation.call(this, '');       },
  • #26: function extend(o,p){ for(prop in p){o.prototype[prop]=p[prop]; }; return o;}Then:extend(calculator, {       numberClick: numberClick,       setOperator: setOperator,       clearNumbers: clearNumbers})Keith Chadwick