(https://scotch.io/)
(https://scotch.io/bar-talk/4-javascriptdesign-patterns-you-should-know#)
4 JavaScript Design
Patterns You Should
Know (+ Scotchmas Day
2)
Devan Patel ( (https://scotch.io/author/devan)@devanp92 (https://twitter.com/devanp92))
December 15, 2015
54 (https://scotch.io/bar-talk/4-javascript-design-patterns-you-should-
know#disqus_thread)
javascript
(https://scotch.io/tag/javascript)
911
shares
0
911
(https://scotch.io/bar(https://scotch.io
talk/4- talk/4javascriptjavascriptdesign- designfile:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Day
1/44
21/12/2015
patterns-patternsyou-
you-
should- should-
ttps://scotch.io/bar-
know#) know#)
avascript-design-
-you-should-know#)
ps://scotch.io/bar-
avascript-design-
-you-should-know#)
AD
Design Pattern
(https://www.fa
(https://tw
(http
more
should-know#)
important as
(https://scotch.io/boo
your-first-node-js-
(https://scotch.io/books
your-first-node-js-a
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Day
2/44
21/12/2015
Email Address
challenge providing an
Subscribe
POPULAR ON SCOT
Creating Deskto
applications.
Applications Wi
AngularJS and G
Electron
(https://scotch.io/tutorials/cr
desktop-applications-with-an
and-github-electron)
Create a MEAN
Google Map Ap
(https://scotch.io/tutorials/m
mean-apps-with-google-map
Containerized T
Node Applicatio
Dockunit
(https://scotch.io/tutorials/co
testing-for-node-applications
dockunit)
Aesthetic Sass 3
Typography and
Rhythm
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Day
3/44
21/12/2015
(https://scotch.io/tutorials/ae
sass-3-typography-and-vertic
following:
rhythm)
Create a Custom
Player Element
Module
Polymer
Prototype
(https://scotch.io/tutorials/cr
Observer
custom-audio-player-elemen
Singleton
polymer)
Create a Real-T
Shoutbox with L
Events
(https://scotch.io/tutorials/cr
real-time-shoutbox-with-lara
key points:
events)
Simbla: A Next
Generation Res
Website Builder
1. Context: Where/under
what circumstances is
(https://scotch.io/tutorials/sim
next-generation-responsive-
builder)
trying to solve?
3. Solution: How does
using this pattern solve
our proposed problem?
4. Implementation: What
does the
implementation look
like?
Module Design
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Day
4/44
21/12/2015
Module Design
Pattern
(https://scotch.io/bartalk/4-javascriptdesign-patternsyou-shouldknow#moduledesign-pattern)
JavaScript modules are the
most prevalently used
design patterns for keeping
particular pieces of code
independent of other
components. This provides
(http://bit.ly/1FxJ3
A VAGRANT LAMP ST
THAT JUST WORK
(http://bit.ly/1PY0
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Day
5/44
21/12/2015
DEAD SIMPLE
OFF-CANVAS PANE
(http://bit.ly/1QJLL
SCOTCH.IO STICKER
(function() {
// declare private variables and/or functions
return {
// declare public variables and/or functions
}
})();
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Day
6/44
21/12/2015
(http://shop.scotch
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Day
7/44
21/12/2015
THE NEWSLETTER
return {
callChangeHTML: function() {
changeHTML();
(https://scotch.io/books/bu
first-node-js-app)
Email Address
console.log(contents);
}
};
})();
HTMLChanger.callChangeHTML();
// Outputs: 'contents'
console.log(HTMLChanger.contents); // undefined
Notice that
callChangeHTML binds to
Revealing Module
(http://synd.co/1Oj5DiJ)
(https://www.facebook
(https://twitter.co
(https://feed
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Day
8/44
21/12/2015
Revealing Module
Pattern
A variation of the module
pattern is called the
Revealing Module Pattern.
The purpose is to maintain
privacy for all variables and
methods only finally
revealed in the returned
object literal. The direct
implementation looks like
this:
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Day
9/44
21/12/2015
Exposer.first();
// Output: This is a method I want to e
Exposer.second();
// Output: Inside a private method!
Exposer.methodToExpose; // undefined
10/44
21/12/2015
Prototype Design
Pattern
(https://scotch.io/bartalk/4-javascriptdesign-patternsyou-shouldknow#prototypedesign-pattern)
Any JavaScript developer has
either seen the keyword
prototype, confused by the
prototypical inheritance or
implemented prototypes in
their code. The Prototype
design pattern relies on the
JavaScript prototypical
inheritance
(https://developer.mozilla.or
g/enUS/docs/Web/JavaScript/Inh
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
11/44
21/12/2015
eritance_and_the_prototype_
chain). The prototype model
is used mainly for creating
objects in performanceintensive situations.
The objects created are
clones (shallow clones) of
the original object that are
passed around. One use
case of the prototype
pattern is performing an
extensive database
operation to create an object
used for other parts of the
application. If another
process needs to use this
object, instead of having to
perform this substantial
database operation, it would
be advantageous to clone
the previously created
object.
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
12/44
21/12/2015
Client
Prototype
imp o rt
o p eratio n ()
clo n e()
ConcretePrototype1
ConcretePrototype2
clo n e()
clo n e()
retu rn co p y o fself
retu rn co p y o fself
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
13/44
21/12/2015
= 'Model S';
}
TeslaModelS.prototype.go = function()
// Rotate wheels
}
TeslaModelS.prototype.stop = function(
// Apply brake pads
}
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
14/44
21/12/2015
= 'Model S';
}
TeslaModelS.prototype = {
go: function() {
// Rotate wheels
},
stop: function() {
// Apply brake pads
}
}
Revealing Prototype
Pattern
Similar to Module pattern,
the Prototype pattern also
has a revealing variation. The
Revealing Prototype Pattern
provides encapsulation with
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
15/44
21/12/2015
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
16/44
21/12/2015
= 'Model S';
}
TeslaModelS.prototype = function() {
var go = function() {
// Rotate wheels
};
var stop = function() {
// Apply brake pads
};
return {
pressBrakePedal: stop,
pressGasPedal: go
}
}();
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
17/44
21/12/2015
prototypical inheritance,
there is no need to rewrite
underlying features.
Observer Design
Pattern
(https://scotch.io/bartalk/4-javascriptdesign-patternsyou-shouldknow#observerdesign-pattern)
There are many times when
one part of the application
changes, other parts needs
to be updated. In AngularJS,
if the $scope object
updates, an event can be
triggered to notify another
component. The observer
pattern incorporates just
that if an object is modified
it broadcasts to dependent
objects that a change has
occurred.
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
18/44
21/12/2015
19/44
21/12/2015
// Controller 1
$scope.$on('nameChanged', function(event
$scope.name = args.name;
});
...
// Controller 2
$scope.userNameChanged = function(name
$scope.$emit('nameChanged', {name:
};
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
20/44
21/12/2015
21/44
21/12/2015
is implemented:
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
22/44
21/12/2015
23/44
21/12/2015
Publish/Subscribe
The Publish/Subscribe
pattern, however, uses a
topic/event channel that sits
between the objects wishing
to receive notifications
(subscribers) and the object
firing the event (the
publisher). This event
system allows code to define
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
24/44
21/12/2015
application-specific events
that can pass custom
arguments containing values
needed by the subscriber.
The idea here is to avoid
dependencies between the
subscriber and publisher.
This differs from the
Observer pattern since any
subscriber implementing an
appropriate event handler to
register for and receive topic
notifications broadcast by
the publisher.
Many developers choose to
aggregate the
publish/subscribe design
pattern with the observer
though there is a distinction.
Subscribers in the
publish/subscribe pattern
are notified through some
messaging medium, but
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
25/44
21/12/2015
Singleton
(https://scotch.io/bartalk/4-javascriptdesign-patternsyou-shouldknow#singleton)
A Singleton only allows for a
single instantiation, but
many instances of the same
object. The Singleton
restricts clients from
creating multiple objects,
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
26/44
21/12/2015
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
27/44
21/12/2015
28/44
21/12/2015
})();
getInstance method is
public. Each officer worker
can generate a printer
instance by interacting with
the getInstance method, like
so:
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
29/44
21/12/2015
Conclusion
(https://scotch.io/bartalk/4-javascriptdesign-patterns-
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
30/44
21/12/2015
design-patternsyou-shouldknow#conclusion)
Design patterns are
frequently used in larger
applications, though to
understand where one
might be advantageous over
another, comes with
practice.
Before building any
application, you should
thoroughly think about each
actor and how they interact
with one another. After
reviewing the Module,
Prototype, Observer, and
Singleton design patterns,
Scotchmas Day 2
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
31/44
21/12/2015
Scotchmas Day 2
Giveaway
(https://scotch.io/bartalk/4-javascriptdesign-patternsyou-shouldknow#scotchmasday-2-giveaway)
IT'S
OVER!
2,137
0/11
Scotchmug
Thiscontestisnolongeracceptingentries.
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
32/44
21/12/2015
(https://scotch.io/author/devan)
DEVAN PATEL
(HTTPS://SCOTCH.IO/AUTHOR/DEVAN)
(@DEVANP92
(HTTPS://TWITTER.COM/DEVANP92))
I'm an avid Javascript developer
which I write about here
(http://www.devanpatel.me/). Feel
free to reach out to me on my
Twitter
(https://twitter.com/devanp92) or
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
33/44
21/12/2015
READ NEXT
(https://scotch.io/tutorials/building(https://scotch.io/baryour-own-
talk/s-o-l-i-d-
javascript-
the-first-five-
modal-plugin)
principles-ofobject-
BUILDING
S.O.L.I.D:
YOUR OWN
THE FIRST 5
JAVASCRIPT
PRINCIPLES
MODAL
OF OBJECT
PLUGIN
ORIENTED
(HTTPS://SCOTCH.IO/TUTORIALS/BUILDINGDESIGN
YOUR-OWN(HTTPS://SCOTCH.IO/BARJAVASCRIPTTALK/S-O-LMODALI-D-THEPLUGIN)
FIRST-FIVEPRINCIPLESOF-OBJECTORIENTEDDESIGN)
(https://scotch.io/bar(https://scotch.io/bar-
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
34/44
21/12/2015
(https://scotch.io/bar(https://scotch.io/bartalk/angular-
talk/changes-
material-vs-
and-reasons-
material-
behind-the-
design-lite)
new-scotch-
ANGULAR
CHANGES
MATERIAL
AND
VS.
REASONS
MATERIAL
BEHIND THE
DESIGN LITE
NEW
(HTTPS://SCOTCH.IO/BARSCOTCH.IO
TALK/ANGULARDESIGN
MATERIAL(HTTPS://SCOTCH.IO/BARVSTALK/CHANGESMATERIALANDDESIGN-LITE)
REASONSBEHIND-THENEWSCOTCH-IO)
(https://scotch.io/bar(https://scotch.io/tutorials/googletalk/scotchmas-
material-
day-5-
design-input-
chromecast-
boxes-in-css3)
jedi-bathSCOTCHMAS
GOOGLE
DAY 5
MATERIAL
CHROMECAST,
DESIGN
JEDI BATH
INPUT
ROBES, AND
BOXES IN
STAR WARS
CSS3
CODEPENS
(HTTPS://SCOTCH.IO/TUTORIALS/GOOGLE(HTTPS://SCOTCH.IO/BARMATERIALTALK/SCOTCHMASDESIGNDAY-5CHROMECASTJEDI-BATHfile:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
35/44
21/12/2015
JEDI-BATHROBES-ANDSTAR-WARSCODEPENS)
INPUTBOXES-INCSS3)
(https://leanpub.com/meanmachine)
scotch.io books presents:
MEAN MACHINE
Learn Node, Angular, Express, and
MongoDB from scratch.
No experience necessary.
Learn About the Book (https://leanpub.com/mean-machine)
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
36/44
21/12/2015
SUBSCRIBE
FOLLOW LIKE
+1
(HTTPS://SCOTCH.IO/FEED)
(HTTP://TWITTER.COM/SCOTCH_IO)
(HTTP://WWW.FACEBOOK.COM/SCOTCHDEVELOPM
(HTTP://PLUS.GOOGLE.COM/B/113854128
Comments
Community
Recommend 5
Share
Login
SortbyBest
Jointhediscussion
skube 4daysago
Inyourveryfirstexampleyouhave:
return : {
// declare public variables and/or functions
}
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
37/44
21/12/2015
Shouldn'ttherebenocolon?
2
Reply Share
DevanPatel>skube 6hoursago
HiSkube,
Firstoff,thanksforreadingthearticle.
Youarecorrectthereshouldnotbea
colon.Iwillreeditthepost.
Thankyou!
Reply Share
skube>DevanPatel
6hoursago
Np.Obviously,youknowwhat
youaredoinganditwasa
simpletypo.Ijustpointitoutfor
peoplelikemewhoeasilyget
confused.
Reply Share
SergeyVoloshin 4daysago
JavaScript!It'llruntheworld!
1
Reply Share
gocreating 20hoursago
Javascriptawesome!!
Reply Share
LeonidKolomiytsev 3daysago
Thanks,helpfulinformation)
Reply Share
TarunGarg 4daysago
Javascriptitsthenextbigthing...:D...
varmyFavLanguage="javascript"
Itwillruletheworld...x)
Reply Share
ChrisMathews 4daysago
Javascript,Python&R
Reply Share
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
38/44
21/12/2015
Hisham 4daysago
Java&JS:))
Reply Share
Artsiom 4daysago
JavaScriptrulezzz!:))
Reply Share
AnastasiaSmirnova 4daysago
JSofcourse:)
Reply Share
M.Onurelik 4daysago
Javascript
Reply Share
MiguelLeite 4daysago
JavaScript:)
Reply Share
SerzN1 4daysago
"RevealingPrototypePattern"wrongcode
block!
Reply Share
MarcoGuglielmelli 4daysago
JavaScript!:)
Reply Share
YvesSchleich 4daysago
Agooddayhastostartwithscotch!
Reply Share
Mafisz 4daysago
PHP
Reply Share
Sarah 4daysago
AlwaysbetonJS)
Reply Share
mkdudeja 4daysago
document.getElementById("scotchmasday2
giveaway").innerHTML="JAVASCRIPT"
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
39/44
21/12/2015
Reply Share
EltonVincent 4daysago
JSFTW!
Reply Share
RodrigoMoreno 4daysago
JavascriptFTW!
Reply Share
chadlefort 4daysago
JavaScript!
Reply Share
JD 4daysago
Javascript!!
Reply Share
Rhadow 4daysago
JavaScript!
Reply Share
Yomi 4daysago
Javascriptbaby!
Reply Share
Matt 4daysago
Javascript!
Reply Share
TiagoWinehouse 4daysago
JS..NICE!!
Reply Share
ThibaultMaekelbergh 4daysago
Howistheprototypepatternevenrelevantwith
everythingES6gaveus?
Reply Share
morsaPT 4daysago
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
40/44
21/12/2015
morsaPT 4daysago
HeyDevan.Awesomearticle!
YouhaveanissueontheSingletonpattern
example.Youareusing`instance`on
getInstancemethodandthesameonSingleton
functionwhenIguessyoushouldbeusing
`printerInstance`.Canyoucheck?
Keepupwriting!:)
Reply Share
DevanPatel>morsaPT
6hoursago
HimorsaPT,
Thanksforthekindwords!
Kudostoyouforpointingitout!Itshould
be'printerInstance'andnot'instance'.
Reply Share
riozagreb 4daysago
jsftw
Reply Share
Nick 4daysago
Javascriptforthewin!
Reply Share
DavorB. 4daysago
javascript
Reply Share
DjordjeAndzic 4daysago
Myfavoriteprogramminglanguageisjavascript.
Reply Share
NickGraz 4daysago
MyfavoritelanguageisPHP
Reply Share
SotirisKatsaniotis 4daysago
MyfavoriteprogramminglanguageisPHP
Reply Share
RizqyHidayat 4daysago
justgettingdeeperonjavascript.thanks!
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
41/44
21/12/2015
justgettingdeeperonjavascript.thanks!
Reply Share
Dill 4daysago
DefinitelyJavaScript
Reply Share
FleetNavTech 4daysago
AngularJS.
Reply Share
JonathanObino 4daysago
JSFTW!
Reply Share
NicoleLambon 4daysago
JavaScript,HTML,andCSSFTW!
Reply Share
AmirTugendhaft 4daysago
Python,theloveofmylife
Reply Share
JairoJurado 4daysago
JavaScriptismyfavoritelanguage.Talkabout
FrontandBackEnddevelopmentwithone
language!
Reply Share
JustinSane 4daysago
ChooseJavascriptforsomuchWIN!
Reply Share
Cody 4daysago
JavaScriptiswhatIusemostbutIhavetosayI
amenjoyingRubythemost.
Reply Share
4daysago
JS!
Reply Share
JohnH 4daysago
PHPismylanguageofchoice.
Reply Share
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
42/44
21/12/2015
Reply Share
MarioGmez 4daysago
Myfavoriteprogramminglanguageis
JavaScript!
Reply Share
KostasKapenekakis 4daysago
Thanks!
Reply Share
RenanBorges 4daysago
nice
Reply Share
Loadmorecomments
WHAT'STHIS?
ALSOONSCOTCH
LearnWebRTC:Build
aRealTimeTicTac
Toe(
21comments4daysago
MakingMEANApps
withGoogleMaps(Part
II)
3comments2monthsago
JulieKnowlesI'd
AlexeyZakharov
lovetoteachmyson
Thanksforthe
(https://www.facebook.com/scotchdevelopment)
(https://twitter.com/scotch_io)
(https://plus.google.com/+ScotchIo/posts)
(http://feeds.feedblitz.com/scotch_io)
License (https://scotch.io/license)
Advertise with Us (https://scotch.io/advertise)
About (https://scotch.io/about)
Join Us on Slack (https://scotch-slack.herokuapp.com/)
Store (http://shop.scotch.io/)
Write for Us (https://scotch.io/write-for-us)
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
43/44
21/12/2015
Contact Us (https://scotch.io/contact-us)
JOIN THE NEWSLETTER
Web design/development tutorials and news from around the web.
Email Address
Subscribe
Hire Us (http://bit.ly/18ib8jR)
(https://leanpub.com/mean-machine)
file:///home/drupalio/Escritorio/4%20JavaScript%20Design%20Patterns%20You%20Should%20Know%20(+%20Scotchmas%20Da
44/44