Douglas Crockford has recently been giving a talk called The Better Parts. The gist of the talk is that JavaScript developers should actually avoid a superset of the The Bad Parts, which now includes new
, Object.create
, any from of delegation or inheritence, including ES6 classes, and this
.
Specifically, Crockford recommends defining new object types by using a constructor function that creates and returns a new object, and that uses closure to create private variables and so on. The obvious issue with this is that every instance has each of its methods bound directly to the instance; there's no delegation, and that's memory hungry. Crockford claims that this is a non-issue, because memory is so cheap. I found that pretty unconvincing.
In a case where some type needed a bunch of methods and a very large number of instances might exist at once, surely any decent JavaScript developer will put those methods in an object, and have every instance delegate to it. It then seems reasonable to just use that kind of one step delegation by default. This goes against the recommendations in The Better Parts, and as soon as somebody says "why not make that delegation recursive", we're back to where we began.
Obviously, if you are creating a very large number of instances of anything, you'd use delegation as an obvious optimisation, but we're normally talking about relatively small numbers, so for that case, can we just forget the costs?
What are the runtime costs of following Crockford's advice, specifically regarding avoiding delegation? On the kinds of consumer devices that typically run web browsers, are the costs generally minor?
==
and!=
operators, while there are actual cases where I do want type-agnostic [in]equality? Crockford's pieces of advice should be mandatory for any beginner and are highly recommended for intermediate programmers, and are good for 99.9% of situations you encounter. When you face the remaining 0.1% of the situations and you know the language enough, you're free to go against what is told in Crockford's books and talks.