[url=http:/civis/viewtopic.php?p=27967637#p27967637:3cg16smk said:
Sleepperi[/url]":3cg16smk]
[url=http:/civis/viewtopic.php?p=27895197#p27895197:3cg16smk said:
Mile Zero[/url]":3cg16smk]Some people will recommend Crockford's JavaScript: The Good Parts. That's a really good book, even if (in retrospect) it's wrong about a great many things. But it's a hard place to start for beginners.
Can you elaborate on that - what kinds of things does it get wrong? Any books/weblinks discussing that stuff?
Crockford's book was important for a lot of JS devs, because it laid out for a lot of people how the functional side of the language worked, and provided tools for thinking about how to use that to provide the kinds of things that it really should have added long ago (privacy, modularity, etc.). I spent about two weeks working my way through it when I first got a copy, and it made me a much better coder.
The problem with a lot of his advice is that, in this day and age, it's not actually very good for performance or memory:
- Modern JS engines optimize object property lookup using hidden classes, and the best way to give them good hints about what those classes should look like means using the new keyword, and not being afraid of prototypical inheritance. Crockford's emulation of class-based inheritance causes de-optimized code, because the VM can't predict the shape of the object as efficiently.
- The Good Parts was a big proponent of creating "private" properties by closing over variables in the constructor with getter/setter functions. Invariably, this means moving more stuff into the constructor, because that's the only way to give it access to that private scope. Turns out this is also terrible for memory/performance, because every time the constructor runs it's creating a unique version of all those variables and functions--they aren't shared through the prototype.
- The book does a lot of augmenting built-in prototypes, which we now know is just generally a bad idea. It's usually safe to shim Function methods, and I usually shim the Array methods if I'm forced to work in a pre-ES5 browser, but adding stuff to Object.prototype is just asking for trouble.
I don't mean this as a way to bash Crockford--I still think The Good Parts is a fantastic book. I teach JavaScript classes at the local community college in my spare time, and if they ever give me an advanced class, we'll probably use it as the textbook. It was highly influential, and parts of it (like the module pattern) are still fundamental to good JS development. But it's best taken with a big grain of salt, especially as the runtime has changed--we have a lot of features available to us in JavaScript now that weren't there when The Good Parts was written, and the underlying tech has gotten a lot more sophisticated.