r/webdev • • Dec 20 '15

4 JavaScript Design Patterns You Should Know

https://scotch.io/bar-talk/4-javascript-design-patterns-you-should-know
115 Upvotes

19 comments sorted by

23

u/[deleted] Dec 20 '15

Very poorly written. See Addy Osmani's patterns for the real deal

1

u/jewdai Dec 20 '15

Addi is a great person to follow. I've learned so much about the chrome dev tools in 5 minutes of watching a video of him.

-3

u/erishun expert Dec 20 '15 edited Dec 20 '15

It's not poorly written, it's a good, simple, short breakdown of the gist. Obviously it's not the be-all end-all, but it's not trying to be.

6

u/[deleted] Dec 20 '15

Since we are returning an object, we will prefix the prototype object with a function

Erm... what? You're wrapping it in a function, and yes, it DOES make a difference.

The section on the observer pattern and MVC was just plain wrong, which is when I stopped reading.

0

u/[deleted] Dec 20 '15

[deleted]

2

u/[deleted] Dec 20 '15

Never thought I'd say /r/javascript has acted as the voice of sanity.

6

u/jewdai Dec 20 '15

prototype is not a pattern. prototype is fundamental to OO in JavaScript.

1

u/[deleted] Dec 20 '15

Yeah hes actully showing a constructor pattern, but fails to ever tell you how to invoke it with 'new'

3

u/[deleted] 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

u/[deleted] 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

u/[deleted] Dec 20 '15

Auto correct.

Yes, but babel is easy enough. Give it a try, so much more fun than es5

3

u/a-t-k Dec 20 '15

Small hint: you can use native browser events (CustomEvent) for the observer pattern.

2

u/[deleted] 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

u/jacquesdancona Dec 20 '15

Why are they obsolete?

0

u/[deleted] Dec 20 '15

[deleted]

3

u/amcsi Dec 20 '15

-var :)