SlideShare a Scribd company logo
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

What's hot (20)

Alert
Alert
3$'"/s/-->*/\xss$`'<x/$&quot%22%27$!{1}&apos;/./\
 
Building complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and React
Jonne Kats
 
“iOS 11 в App in the Air”, Пронин Сергей, App in the Air
“iOS 11 в App in the Air”, Пронин Сергей, App in the Air
AvitoTech
 
Ngrx slides
Ngrx slides
Christoffer Noring
 
Event-Driven Systems With MongoDB
Event-Driven Systems With MongoDB
Andrii Litvinov
 
Utilising the data attribute
Utilising the data attribute
Richard Martens
 
Rapid prototyping of eclipse rcp applications - Eclipsecon Europe 2017
Rapid prototyping of eclipse rcp applications - Eclipsecon Europe 2017
Patrik Suzzi
 
SUGCon 2014 Sitecore MVC
SUGCon 2014 Sitecore MVC
mikeedwards83
 
Apple.combine
Apple.combine
mober77
 
jQuery - Chapter 4 - DOM Handling
jQuery - Chapter 4 - DOM Handling
WebStackAcademy
 
Seeking the truth from mobile analytics
Seeking the truth from mobile analytics
Mouhcine El Amine
 
From zero to hero with the reactive extensions for java script
From zero to hero with the reactive extensions for java script
Maurice De Beijer [MVP]
 
Advanced JavaScript Concepts
Advanced JavaScript Concepts
Naresh Kumar
 
Ngrx: Redux in angular
Ngrx: Redux in angular
Saadnoor Salehin
 
jQuery - Chapter 5 - Ajax
jQuery - Chapter 5 - Ajax
WebStackAcademy
 
Wix Automation - DIY - Testing BI Events
Wix Automation - DIY - Testing BI Events
Efrat Attas
 
9781305078444 ppt ch10
9781305078444 ppt ch10
Terry Yoast
 
Flux - new/old approach to building frontend
Flux - new/old approach to building frontend
Bartek Witczak
 
Wix Automation - Core
Wix Automation - Core
Efrat Attas
 
Resiliency & Security_Ballerina Day CMB 2018
Resiliency & Security_Ballerina Day CMB 2018
Ballerina
 
Building complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and React
Jonne Kats
 
“iOS 11 в App in the Air”, Пронин Сергей, App in the Air
“iOS 11 в App in the Air”, Пронин Сергей, App in the Air
AvitoTech
 
Event-Driven Systems With MongoDB
Event-Driven Systems With MongoDB
Andrii Litvinov
 
Utilising the data attribute
Utilising the data attribute
Richard Martens
 
Rapid prototyping of eclipse rcp applications - Eclipsecon Europe 2017
Rapid prototyping of eclipse rcp applications - Eclipsecon Europe 2017
Patrik Suzzi
 
SUGCon 2014 Sitecore MVC
SUGCon 2014 Sitecore MVC
mikeedwards83
 
Apple.combine
Apple.combine
mober77
 
jQuery - Chapter 4 - DOM Handling
jQuery - Chapter 4 - DOM Handling
WebStackAcademy
 
Seeking the truth from mobile analytics
Seeking the truth from mobile analytics
Mouhcine El Amine
 
From zero to hero with the reactive extensions for java script
From zero to hero with the reactive extensions for java script
Maurice De Beijer [MVP]
 
Advanced JavaScript Concepts
Advanced JavaScript Concepts
Naresh Kumar
 
jQuery - Chapter 5 - Ajax
jQuery - Chapter 5 - Ajax
WebStackAcademy
 
Wix Automation - DIY - Testing BI Events
Wix Automation - DIY - Testing BI Events
Efrat Attas
 
9781305078444 ppt ch10
9781305078444 ppt ch10
Terry Yoast
 
Flux - new/old approach to building frontend
Flux - new/old approach to building frontend
Bartek Witczak
 
Wix Automation - Core
Wix Automation - Core
Efrat Attas
 
Resiliency & Security_Ballerina Day CMB 2018
Resiliency & Security_Ballerina Day CMB 2018
Ballerina
 

Viewers also liked (13)

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

Similar to JavaScript Patterns to Cleanup your Code (20)

Javascript design patterns
Javascript design patterns
GomathiNayagam S
 
Java Script Patterns
Java Script Patterns
Allan Huang
 
JavaScript (without DOM)
JavaScript (without DOM)
Piyush Katariya
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
Ajax Experience 2009
 
Ch 7: Object-Oriented JavaScript
Ch 7: Object-Oriented JavaScript
dcomfort6819
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)
Addy Osmani
 
Advanced JavaScript
Advanced JavaScript
Zsolt Mészárovics
 
Goodparts
Goodparts
damonjablons
 
JavaScript Patterns
JavaScript Patterns
Giordano Scalzo
 
Java scriptpatterns
Java scriptpatterns
Francesca1980
 
Java scriptpatterns
Java scriptpatterns
Francesca1980
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
Ravi Bhadauria
 
Wakanday JS201 Best Practices
Wakanday JS201 Best Practices
Juergen Fesslmeier
 
Javascript fundamentals and not
Javascript fundamentals and not
Salvatore Fazio
 
JavaScript Proven Practises
JavaScript Proven Practises
Robert MacLean
 
JavaScript: Patterns, Part 1
JavaScript: Patterns, Part 1
Chris Farrell
 
java script functions, classes
java script functions, classes
Vijay Kalyan
 
Intermediate JavaScript
Intermediate JavaScript
☆ Milan Adamovsky ☆
 
The mighty js_function
The mighty js_function
timotheeg
 
Advanced JavaScript
Advanced JavaScript
Nascenia IT
 
Javascript design patterns
Javascript design patterns
GomathiNayagam S
 
Java Script Patterns
Java Script Patterns
Allan Huang
 
JavaScript (without DOM)
JavaScript (without DOM)
Piyush Katariya
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
Ajax Experience 2009
 
Ch 7: Object-Oriented JavaScript
Ch 7: Object-Oriented JavaScript
dcomfort6819
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)
Addy Osmani
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
Ravi Bhadauria
 
Javascript fundamentals and not
Javascript fundamentals and not
Salvatore Fazio
 
JavaScript Proven Practises
JavaScript Proven Practises
Robert MacLean
 
JavaScript: Patterns, Part 1
JavaScript: Patterns, Part 1
Chris Farrell
 
java script functions, classes
java script functions, classes
Vijay Kalyan
 
The mighty js_function
The mighty js_function
timotheeg
 
Advanced JavaScript
Advanced JavaScript
Nascenia IT
 
Ad

Recently uploaded (20)

Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
AudGram Review: Build Visually Appealing, AI-Enhanced Audiograms to Engage Yo...
SOFTTECHHUB
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 

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, &apos;0&apos;);           setEquation.call(this, &apos;&apos;);       },
  • #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