Hacker News new | ask | show | jobs
by Ramone 5022 days ago
Looks more like JS the java way. Singletons make no sense in a language that has global variables and never lets you stop an object from being duplicated. The command pattern makes no sense in a language with first-class functions. I'd steer clear of this resource if you want to write javascript the right way.
6 comments

I somewhat agree with your points.

You can't just explain every single design pattern available in any given language and then say, you now know how to write that language "The right way!".

Thats the feeling I got from this, it just goes into way too much detail about way too many things, and forgets to start with the basics, which is all you need to know to write JavaScript the right way.

How can you write any language the right way without knowing the basics?

This book could be an absolute disaster in the hands of people who don't know any better.

+1 for steering clear of this resource.

Yeah. I was expecting things like "use semicolons", "global variables are bad", "=== is often what you want, not ==". These are the things that many devs will need to learn. A bunch of design patterns was not what I expected.
While I agree with the fact that the title is misleading, I would have been even more disappointed if it was just a restating of "JavaScript: The Good Parts".
Intermediate JS devs need online resources too. Imo, an in-depth explanation of inheritance and the module pattern would be the best possible resources for people at that level of the learning curve. In other words, just read Doug Crockford.
For explanation of JS inheritance I would recommend reading http://killdream.github.com/blog/2011/10/understanding-javas..., it's actually superior to what you will find in Crocoford's book.
Almost every javascript library you use is a singleton, so your second sentence makes no sense. Check the example, it is just the module pattern. If a programmer wants to deviate away from a pattern, the language lock-in won't stop them.

Also, the command pattern is a great tool for handling undo/redo on a UI. I think you are being way to dismissive of some good advice. Just because someone is trying to share a better way of doing things that is based on ideas created in another language does not detract from their value.

Can you give an example of a JavaScript library that is a singleton?
In JS, a singleton is a reference to a module. It is used to ensure you invoke the module only once.

Global variables are irrelevant since your global namespace most likely resembles a tree structure.

edit: you may be right about the command pattern, I've never used something like that. It may just be the example is half-assed though.

There are no modules in JavaScript as well. At least not in ES5.
Module pattern. It is easily one of Javascripts greatest strengths.
It's a very useful hack but I would hardly call it a great strength. As a module system it completely sucks.

It does leverage one of the language's great strengths, lexically-scoped first-class functions, but that doesn't make it a strength on its own.

Not sure if anyone will ever see this...

It is not a traditional module I will agree. However I see it as a hybrid class/interface/module and in this role, it works very well and allows you a tremendous amount of flexibility due to the dynamicness of js.

design patterns have completely failed. Sometimes, they appear in my code, but knowing them has never helped me to code better. What helps me is to know language idioms and code patterns.

It is a pity that so many teachers give such a devotion to design patterns.

I used to think as you do, and I still think that too much emphasis is placed on patterns, and too many poor coders stitch them together in complex ways to achieve exceedingly simple tasks.

However, when part of your design begins to take a form similar to a well-known pattern, there is a good reason to convert your design to use the specific pattern if possible: communicating with other programmers (including yourself, six weeks later). It means that instead of bogging down in documentation of exactly how that component works, you can just say "it's this pattern", and everyone familiar with the pattern will instantly know how it works, minus a few details.

In addition, knowing about patterns and seeing how they're used in practice (rather than toy examples that make them seem masturbatory) can help you to reason about problems in the future. Not because you necessarily use those specific patterns, but because you have understood how they achieve what they achieve.

Care to point us to some better resources?
I've shipped very little JS code, so take this with a grain of salt. However I have had to ship JS code, and since I'd heard horror stories, I tried to find some solid resources to make sure I wasn't shipping garbage. I found these really helpful:

Eloquent JavaScript, a freely available book with an in-browser REPL and a lot of fun exercises

http://eloquentjavascript.net/

Douglas Crockford's JS master class, which covers a lot of the same material as his "JavaScript: the Good Parts" but in video format, and I think in a much more digestible way. The Good Parts book is very, very dense and probably best used as a reference. (why he thought starting with a formal definition of the langauge's grammar was a good idea completely escapes me)

http://shop.oreilly.com/product/9780596809614.do

I don't actually know if these are really up-to-date, and all the cool kids are using CoffeeScript anyway, so anyone with more experience please correct me if these aren't any good. They definitely point out how to avoid the most painful quirks of the language, and how to leverage the core that is actually decent.