Using object constructors that require new
is something of an anti-pattern, despite being very well established. Numerous posts and questions address this topic. Generally, objections to new
come in one of a few flavors:
- It masks the underlying prototypal inheritance mechanism by borrowing terminology from classical OO. The "JavaScripty" way is to embrace prototypal inhertiance, rather than use misleading sugar like
new
(or the upcoming es6class
syntax). - It tightly couples clients to the constructor implementation.
- It is dangerous in that a function intended to be called with
new
may spam the global namespace when called withoutnew
. - It makes it awkward to do things like partial application, or use an array of arguments with
.apply
However, it's fairly straightforward to write factory functions -- object-building functions intended to be called without new
-- that still support calling with new
and even properly identify produced objects as instanceof
the factory. As such, consumers are free to use these functions however they please. Perhaps they prefer the way capitalized constructors and new
appear in their specific editor and syntax highlighting; that's fine. They can simply
var Hello = require("helloFactory");
var hi = new Hello();`
Alternately, they can do:
var helloFactory = require("helloFactory");
var hi = helloFactory();