Ask HN: Learning to write good JavaScript
Check out the code in the open source project fabric.js <a href="http://fabricjs.com/" rel="nofollow">http://fabricjs.com/</a> . It's written by Juriy Zaytsev who was a maintainer for prototype.js. I find the code useful for learning advanced object oriented JavaScript because:<p>1) It's self contained. No dependencies on third party OO libraries or frameworks.<p>2) The code is very readable.<p>3) It's a canvas library, so it's fun to work with if you're into graphics and visualizations.<p>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.<p>Here is what I would do:<p>1) Check out the project and get it building.<p>2) Read all the files in the util folder. You'll see a lot of methods added to Object and Array.<p>3) Take a look at lang_class.js: <a href="https://github.com/kangax/fabric.js/blob/master/src/util/lang_class.js" rel="nofollow">https://github.com/kangax/fabric.js/blob/master/src/util/lan...</a> This is how he does OO JS. It's very similar to John Resig's OO classes: <a href="http://ejohn.org/blog/simple-javascript-inheritance/" rel="nofollow">http://ejohn.org/blog/simple-javascript-inheritance/</a> and somewhat different to prototype's classes: <a href="https://github.com/sstephenson/prototype/blob/master/src/prototype/lang/class.js" rel="nofollow">https://github.com/sstephenson/prototype/blob/master/src/pro...</a><p>4) Then take a look at the base class: <a href="https://github.com/kangax/fabric.js/blob/master/src/object.class.js" rel="nofollow">https://github.com/kangax/fabric.js/blob/master/src/object.c...</a> and an inherited class: <a href="https://github.com/kangax/fabric.js/blob/master/src/line.class.js" rel="nofollow">https://github.com/kangax/fabric.js/blob/master/src/line.cla...</a><p>5) Search for instances of the "bind" method, and see how they're used.<p>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 <a href="http://shop.oreilly.com/product/9780596805531.do" rel="nofollow">http://shop.oreilly.com/product/9780596805531.do</a><p>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.