10 biggest JS pitfalls
brendan at mozilla.com
Sun Dec 30 14:43:10 PST 2012
Axel Rauschmayer wrote:
> 1. Both undefined and null [not too much of a problem, easily learned]
> 2. Truthy and falsy values [not pretty, but I’ve grown used to them
> and the convenient but sloppy “has a value” checks via `if`]
These are usually winning. Dart makes you be explicit in testing and
convering, it's a pain from what I can tell.
ToBoolean implicit conversions usually DWIM. The ToNumber, ToInt32, and
ToUint32 conversions more often go wrong.
> 3. == [easy fix: always use ===]
> 4. Parameter handling [fixed in ES6]
> 5. Array-like objects [not completely fixed (DOM...), but `arguments`
> becomes obsolete in ES6]
> 6. Function-scoped variables [`let` FTW in ES6]
> 7. Accidental sharing of data [for-of will help in ES6]
> 8. Creating sub-constructors is messy [fixed via classes and `super`
> in ES6]
> 9. `this` in non-method functions:
> 9a) Referring to the `this` of a surrounding method,
> 9b) accidentally creating globals by forgetting `new`,
> 9c) using methods as callback functions
> [(a) and (b) fixed by ES6 arrow functions and ES5 strict mode]
> 10. The for-in loop [simple rule: avoid if you can, already possible
> in ES5]
> Thus: 1-3 won’t go away soon. 4-10 are mostly eliminated by ES6.
> Deliberate omissions:
> - Implicit conversions are messy (and a superset of pitfall #2), but
> seem to surprise people much less than the above items.
I think you have it backwards.
> consider that hole a pitfall.
You mean free variables being static errors? That is not a done deal yet.
Missing properties yielding undefined still burns many JS programmers.
Not an implicit conversion, so worth a separate item.
More information about the es-discuss