another guard against omitting the new keyword
lewisje opened this issue · 0 comments
lewisje commented
This is well-known but may also be considered an antipattern, especially because it cannot be encapsulated, and instead the constructor name must be updated in three places, and the argument list in two, if either is changed, all because Function#apply cannot perform a constructor call:
function Constructor(arg, arg2) {
if (!(this instanceof Constructor)) {
return new Constructor(arg, arg2);
}
// rest of constructor code
}