In his "Good Parts," Crockford suggests that 'new' should never be used. To follow that rule, how would you refactor the following code?
function Range(from, to) {
this.from = from;
this.to = to;
}
Range.prototype = {
includes: function(x) {
return this.from <= x && x <= this.to;
},
foreach: function(f) {
for(var x = Math.ceil(this.from); x <= this.to; x++) f(x);
},
toString: function() {
return "(" + this.from + "..." + this.to + ")";
}
};
// Here are example uses of a range object
var r = new Range(1,3); // Create a range object
r.includes(2); // => true: 2 is in the range
r.foreach(console.log); // Prints 1 2 3
I spotted his additional advice, but it wasn't clear how to apply it in this (presumably very common) case. Would he propose to create a factory function that contains a giant object literal? If yes, isn't that inefficient? ISTM that such a factory function, upon each invocation, creates duplicate functions. In other words, there is no one prototype holding shared custom methods.
It seems something is left unsaid in his advice, I'm hoping someone can clear it up.