1

I have seen some articles and some claims on answers in SO questions that using new and constructor functions is wrong and should not be used.

They go against the prototype nature of JavaScript.

Could someone please enlighten me and show a situation where using new and constructor functions is so bad it should never be used?

7
  • 2
    new's just another tool in the programmer's bag -- don't be afraid to use them where relevant. Same goes for other things like eval and with (though cases for the latter are rare). Commented Feb 10, 2014 at 5:02
  • 2
    Could you link to those articles? Commented Feb 10, 2014 at 5:02
  • Before Object.create, there really was no replacement for new, if one wanted prototypal support. Commented Feb 10, 2014 at 5:03
  • @user2864740—there is a pollyfill for Object.create on MDN that uses a pattern crated at least a decade ago (originally called "clone"). There was also direct assignment to __proto__ (in some browsers), but that's been superceded by other methods. Commented Feb 10, 2014 at 5:29
  • @RobG The polyfill requires new and __proto__ has never been remotely standardized. Commented Feb 10, 2014 at 5:53

1 Answer 1

2

using new and constructor functions is wrong and should not be used.

Read Is JavaScript's "new" keyword considered harmful? - No, it is not. A few (correct) arguments are

  • It's confusing to newbies because of hiding the prototypical concept. To quote @Aadit:

    [With new] the constructor function becomes simpler. However it becomes very difficult to explain prototypal inheritance to a person who knows nothing about it. It becomes even more difficult to explain it to a person who knows classical inheritance.

  • Constructors do silently fail when forgetting new
  • In a few instances, the pure Object.create approach is cleaner
  • Building a class hierarchy is complicated and often done wrong

However, once you understand these, new is harmless. Actually, every time you need instance initialisation plus prototypical inheritance, constructors with new are the way to go.

They go against the prototype nature of JavaScript.

This will never change. I hardly can imagine why anyone would criticise this, prototypical inheritance is far more powerful than class inheritance. Probably they are only arguing against the syntax.

Could someone please enlighten me and show a situation where using new and constructor functions is so bad it should never be used?

It should not be used when not needed. Singletons can easily be created using the module pattern and object literals; inheritance does not help here. See Is it right to think of a Javascript Function Expression that uses the 'new' keyword as 'static' for an example.

Sign up to request clarification or add additional context in comments.

9 Comments

But one can use new without inheritance so-to-speak; and using the prototype "saves properties" (albeit perhaps not a valid argument with modern JS implementations).
@user2864740: No. new always does always create a new object with a custom inheritance chain - or do you mean new Object by "without inheritance"?
Thank you Bergi, sorry to post this here but answers have been popping up lately referring to articles or directly advising not to use new. Maybe I missed some new development and should advice users asking for information about prototype to not use new but I didn't think so. A question including the problems I have with these statements was closed stackoverflow.com/questions/21669415/…
@Bergi The following answer stackoverflow.com/a/21654362/1641941 addresses the OP's question (correction given in a comment) then goes on to using "helper" functions with the benefit that "you don't have to understand prototype to use it" and has a link to the article that will tell you it's bad to use new (twice) First comment on your answer stackoverflow.com/a/21609992/1641941 the link to DC's article is confusing but makes several claims "classical inheritance" is no good and links to how DC uses it, I did comment that the code used is indeed no good but isn't the way to do it
@HMR: Ah, you refer to aaditmshah.github.io/why-prototypal-inheritance-matters/… - you could've pointed there directly :-) Actually I think he is right. It's all about understanding how it works - and new does not help learners. However, it's a powerful tool for the gurus. So draw your own conclusions from that - are you (and your team) ready to use it?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.