4 ms·
Even side-effect free constructors can make this pattern painful. Consider the case where we're trying to maintain the invariant that all Points always have val
by ef4 14y ago
Even side-effect free constructors can make this pattern painful. Consider the case where we're trying to maintain the invariant that all Points always have valid numbers for all their coordinates:
var Point = function(x, y)
{
if (typeof(x)!='number' || typeof(y)!='number'){
throw new Error("...");
}
this.x = x;
this.y = y;
}
Now you're forced to pick an arbitrary point when setting up the prototype, which is kinda gross.
My point is that people write constructors with the expectation that they'll only be used for constructing meaningful objects.