r/webdev • u/huashoes • Dec 20 '15
4 JavaScript Design Patterns You Should Know
https://scotch.io/bar-talk/4-javascript-design-patterns-you-should-know6
u/jewdai Dec 20 '15
prototype is not a pattern. prototype is fundamental to OO in JavaScript.
1
Dec 20 '15
Yeah hes actully showing a constructor pattern, but fails to ever tell you how to invoke it with 'new'
3
Dec 20 '15
Are these something I should know as an intermediate?
3
u/TrackieDaks Dec 20 '15
Yes, definitely. Next don't take the methods from this article because they are incorrect.
Addy's free online book is the best resource available.
2
u/jackrosenhauer Dec 20 '15
http://addyosmani.com/resources/essentialjsdesignpatterns/book/ Here are a few more, they don't just apply to JavaScript either.
3
u/jamauss Dec 20 '15
Something that I think really should have been pointed out in this article is that it's helpful (and very common actually) to pass in arguments to your IIFE's so that you can use functionality from other libraries inside your own modules as well as split up namespaced functionality across multiple .js files. For example:
var myModule = (function($, lib) {
lib.myModuleFunction = function() {
// do some jQuery stuff with the same $ object syntax you're used to anywhere else
$('#IdSelector').show();
};
return lib;
})(jQuery, myModule || {}); I can now have a myModule reference spread across multiple .js files and as long as I follow this pattern in each file, all of the lib.xxxx functions inside my modules will be available as myModule.xxxx across the entire js codebase.
-4
Dec 20 '15
Looks like a poor man's es6 import
2
u/jamauss Dec 20 '15
"poor man's" ? AFAIK the es6 import feature is supported in exactly zero browsers right now (Dec. 2015)
-2
3
u/a-t-k Dec 20 '15
Small hint: you can use native browser events (CustomEvent) for the observer pattern.
2
Dec 20 '15
"Every developer strives to write maintainable, readable, and reusable code"
If only that were true. Stopped reading after the first paragraph.
-7
u/alexmcmillan Dec 20 '15
Very good patterns to help with your understanding of functional programming in general, with examples that will help with understanding Javascript.
Unfortunately most of these examples are obsolete now.
1
0
23
u/[deleted] Dec 20 '15
Very poorly written. See Addy Osmani's patterns for the real deal