Javascript the right way
jstherightway.com
jstherightway.com
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.
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.
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.
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.
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.
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.
It is a pity that so many teachers give such a devotion to design patterns.
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.
[1] http://twitter.github.com/bootstrap/javascript.html#scrollsp...
His prototype pattern also doesn't look like the right way of doing things. Is there actually anyone that uses plain object descriptors? I would just create helper method that wraps Object.create() and takes prototype object as first argument and instance object as second argument. There is no need to use constructors in this pattern (either directly or inside helpers) unless you are targetting legacy browsers.
I also dislike the fact that public/local scope is emulated by misusing the return statement - this is ugly, confusing and it doesn't play nicely with other patterns such as prototype. If you need some notion of public/private scope then just use underscore notation.
Also, javascript has closures, use them. this._private is a hacky java-esque way of doing things. return {public_methods...} is a solid way of working towards the strengths of the language and doing proper js encapsulation.
As for the prototype pattern, there are a million ways you can do that. Best to learn the fundamentals so you can use whatever works best based on the situation.
edit: I should add that one of the reasons I like JS patterns is that there are multiple correct ways to implement them based on the situation. There are many wrong ways too, of course :) but it is a very flexible language.
mySingleton = {
initialized: false,
init: function() {
if (this.initialized == false) {
this.initialized = true;
// Initialize the singleton here
}
return this
},
}
singleton1 = mySingleton.init()
singleton2 = mySingleton.init()
Both underscore notation and returning "public" methods feel awkward in JavaScript, though the
second appraoch introduces major trade-offs which I belive are not worth it. E.g. how do you create inheritance chains
when using this pattern? Are you just copying public methods returned from one object into another? Or how do you inspect private properties in Dev Tools? Are you setting breakpoints for that purpose?I noticed that PHP The Right Way (the inspiration for this site) says something similar: Based on a work at www.phptherightway.com.
Javascript: The Good Parts is a pretty good book, good real world examples, like one where Crockford shows you how to split a url with regular expressions in javascript
JSlint is a pretty good tool, great parsing, using C, going on in the source
JSON is great for data. Data is pretty important.
One framework I think is missing in the category "server-side" is derby.js ( http://derbyjs.com ), have you thought about adding it to the list?
Sorry, I couldn't resist...
Please, feel free to fork the project and send a pull request, we really want to make this guide a reference for JavaScript developers.