I am comfortable enough with the basic syntax etc, so not looking for absolute beginners stuff, but I would love some recommendations of resources that teach about real world use of the language ... common patterns & anti-patterns, typical coding style etc. A nice book or two would be ideal, but online resources are fine too!
Basically I don't just want to write JavaScript, I want to write good JavaScript.
In that sense, writing good JavaScript is like writing good PHP or good C++, it's a lot about avoiding the broken features (for both those languages, there are a lot of features one shouldn't use, it's even more true with js).
That means you should only learn from people who recognize how broken js is, and thus of course js: the good parts sounds like the right direction, whereas I think Resig is a religious zealot and shouldn't be listened to (the guy actually thinks broken js as a first language is a good idea).
Lastly, js is broken in many ways, don't use it when you don't have to, that means avoid node.js and use a good server side language instead.
tl;dr js sucks, don't listen to people who don't ack that, learn to avoid the sucky parts instead.
And more downvotes from the zealots... HN is so predictable these days.
Now, to add to the real discussion. Aside from all the wonderful suggestions you received so for: once you have read all that you can and broken/learned first hand all you can, and if you are of the pattern persuasion I would look in to the functional language patterns. The Classical patterns, as found in most examples and resources these days, map poorly to JS. At least that is my opinion.
I haven't seen a perfect programming language yet, but the compromises offered by FORTRAN, C, Ada and lisp are really hard to beat, as the first three offer very competitive speed whereas the last one trades off about 50% speed for a lot of built-in metaprogramming.
It's really all about compromises, and JS fits no bill except "easy to learn for noobs", which imho is a non-issue for anyone considering long-term use.
(in case you didn't know, fortran has awesome arrays and awesome compilers, C has inline ASM and awesome compilers, Ada has increased execution security and lisp is lisp)
Look at the top rated posts in the thread, all of them contain a ton of specific advice on avoiding the sucky parts.
Avoid the sucky parts by listening to people who recognize the suck.
And that's exactly why it is an addition to the existing answers, most of which do not focus on that core point, and some of which even advise to read documentation written by people who don't recognize the failures of JavaScript.
1) It's self contained. No dependencies on third party OO libraries or frameworks.
2) The code is very readable.
3) It's a canvas library, so it's fun to work with if you're into graphics and visualizations.
4) You'll learn a lot about how many JS projects are built, documented, and tested, if you get it to build and the tests running on node.js.
Here is what I would do:
1) Check out the project and get it building.
2) Read all the files in the util folder. You'll see a lot of methods added to Object and Array.
3) Take a look at lang_class.js: https://github.com/kangax/fabric.js/blob/master/src/util/lan... This is how he does OO JS. It's very similar to John Resig's OO classes: http://ejohn.org/blog/simple-javascript-inheritance/ and somewhat different to prototype's classes: https://github.com/sstephenson/prototype/blob/master/src/pro...
4) Then take a look at the base class: https://github.com/kangax/fabric.js/blob/master/src/object.c... and an inherited class: https://github.com/kangax/fabric.js/blob/master/src/line.cla...
5) Search for instances of the "bind" method, and see how they're used.
Of course, as you're doing all this, you'll need to experiment with simple language constructs in your browser's console to test what you think you know, and you'll want to read some chapters from a thorough JS book like, JavaScript the Definitive Guide http://shop.oreilly.com/product/9780596805531.do
Finally, this is just a personal opinion, but I don't like Crokford's chapter on OO JS. I just don't think it presents your options well. If you decide to write a large project in JS using OO techniques, I think you'd be better off utilizing an OO library, compiler, or framework like TypeScript, Google Closure, Prototype or CoffeeScript, than you would charging forward armed with Crokford's chapter on OO.
This would have been true a long time back when Lisp was the language with first class functions, closures etc., but today every other languages have those and JS is a lot closer to them Lisp.
Even if this was true, why not learn JS and skip the Lisp part?
> but with better libraries and C-like syntax.
May be with better 3rd party libraries. Out of the box, JS standard library sucks.
My favorites from this site are as follows: http://eloquentjavascript.net/contents.html http://bonsaiden.github.com/JavaScript-Garden/ http://www.addyosmani.com/resources/essentialjsdesignpattern... http://jstherightway.com/
Mozilla has excellent JavaScript material: https://developer.mozilla.org/en-US/docs/JavaScript/Guide https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...
As far as print goes, you'll want these books in your reading list:
JavaScript: The Good Parts by Douglas Crockford Secrets of the JavaScript Ninga by John Resig
On a related note, there was some interesting discussion right here on HN with regards to Addy Osmani's JS Patterns eBook - http://news.ycombinator.com/item?id=2762888
You'll learn a lot from watching their tutorials and how the code is converted to javascript.
However I think I would rather get a strong handle on vanilla JavaScript before I start looking at things like Coffeescript/Typescript.
Eloquent JavaScript: http://eloquentjavascript.net/contents.html
Learning JavaScript Design Patterns: http://www.addyosmani.com/resources/essentialjsdesignpattern...
JS The Right Way: http://jstherightway.com/
Learning Advanced JavaScript: http://ejohn.org/apps/learn/
Ask HN: JavaScript Dev Tools: http://news.ycombinator.com/item?id=3550998
MVC Architecture for JS: http://michaux.ca/articles/mvc-architecture-for-javascript-a...
Large-Scale JS Application Architecture: http://addyosmani.com/largescalejavascript/
Mozilla Developer Network - Intro to OO JS: https://developer.mozilla.org/en-US/docs/JavaScript/Introduc...
I'll also recommend spending a little time with javascript before jumping into jQuery. learning how things are scoped is important.
2. Read good JavaScript (as others have said). The jquery source is a good place to start. Underscore.js is good, too. Unfortunately there isn't consensus about what is and what isn't good JavaScript. Some people seem to be suggesting Prototype as an example of good js, which I would disagree with.
Or jshint!
I assure you, its worth the money.
Note: I am in no way associated with the publishers or author apart from being a beneficiary of this awesome repository of knowledge. This is not even an affiliate link.
GOOD JavaScript is the code that plays nicely with its environment. There is no strict best practice for every situation. You have to learn the language of your neighborhood.
There is only 1 book you should start with, "Javascript, the Good Parts". It only talks about the language, not DOM apis. If I were teaching any student javascript, I would always start with that book.
As for the rest of it, learn the language. Avoid Coffeescript, Typescript, or any other DSL that purports to "compile" to javascript. You'll only be doing yourself a disservice. Just worry about learning the language first. This is vital to understand what's going on.
If you write a lot of js, you'll probably write messy js, but you'll understand how it can so easily get out of hand.
Then read a lot of code and the other resources people have linked to here. Having done a lot from scratch will motivate the solution a lot more.
You will have actually run into the problems they are telling you to solve and understand why it's good js rather than just taking their word for it.
http://beardedocto.tumblr.com/post/21920818233/oo-js-in-15mi...
Very well written, well explained, and it points to many more best practices.
Just do it! Google things as you learn. Try writing a RESTful todo app with Backbone on the client and node on the backend. You'll soon learn async patterns, closures, constructor functions and most importantly understand how `this` works. Those along with `apply` I think are the essentials to being an effective Javascript programmer.
1. Read Douglas Crockford's Javascript: The Good Parts (Alternatively you can watch his lectures on Yahoo Theater)
2. Read Nicholas Zakas' Maintainable Javascript and Scalable Javascript.
Trust be both these books will make a great difference to the way you write your javascript code.
The next step should be to contribute to some open source JS frameworks. I will suggest jQuery or YUI. You need not always write code, you can begin with improvements to the documentation and later move on to submitting actual code.
It is a datagrid developed using pure javascript and jquery. The codebase is one of the best that I have seen in my life so far. Proper use of separation of concerns, responsibilities. Very neat use of oops, methods.
There are lot of examples which will teach you great stuff.