Skip to content

Comment on Stop Using Constructors in JS (2012)

Comments

Here's a counter-argument.

    var x = new Foo()
...is almost universally recognizable, and leaves little guesswork about the intent and meaning of the code. There's a lot more ambiguity encountering this:
    var x = foo()
What am I getting back? A primitive value? An instance? A singleton? I have to go peruse the docs. `new` can definitely be abused to do non-intuitive things, but it's still a powerful signal to future readers of the code.

Name your factory "createFoo", problem solved. :)

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.