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?

HMR
  • 37,593
  • 24
  • 91
  • 160
  • 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). – Qantas 94 Heavy Feb 10 '14 at 05:02
  • 2
    Could you link to those articles? – Felix Kling Feb 10 '14 at 05:02
  • Before `Object.create`, there really was no replacement for `new`, if one wanted prototypal support. – user2864740 Feb 10 '14 at 05:03
  • @user2864740—there is a [pollyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) 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. – RobG Feb 10 '14 at 05:29
  • @RobG The polyfill *requires* `new` and `__proto__` has never been remotely standardized. – user2864740 Feb 10 '14 at 05:53
  • @FelixKling the following was closed, it has some links containing the statements http://stackoverflow.com/questions/21669415/why-would-using-new-and-constuctor-functions-in-javascript-be-wrong/21669527?noredirect=1#21669527 I think its fine to say never do this or always do that but should include that that's an opinion and there is no technical reason – HMR Feb 10 '14 at 06:15
  • @FelixKling wrong link, should be :http://stackoverflow.com/questions/21669081/why-would-using-new-and-constructor-functions-be-bad – HMR Feb 10 '14 at 06:38

1 Answers1

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.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 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 Feb 10 '14 at 05:12
  • @user2864740: No. `new` always does always create a new object with a custom inheritance chain - or do you mean `new Object` by "*without inheritance*"? – Bergi Feb 10 '14 at 05:13
  • Without requiring inheritance-across-types (e.g. just to it's "own" per-type prototype), is what I mean. But that's not running into what I'd consider "problems" with subtype (or, prototypal in this context) inheritance. – user2864740 Feb 10 '14 at 05:14
  • 1
    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 http://stackoverflow.com/questions/21669415/why-would-using-new-and-constuctor-functions-in-javascript-be-wrong/21669527?noredirect=1#21669527 – HMR Feb 10 '14 at 06:11
  • @HMR: I haven't seen any :-) It might help if you could cite and link those articles in your question. In the answer[s], we then could tear their arguments to pieces. Your blog-post-style question was not the appropriate form for this… – Bergi Feb 10 '14 at 13:56
  • 1
    @Bergi The following answer http://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 http://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 Feb 10 '14 at 15:47
  • 1
    @HMR: Ah, you refer to http://aaditmshah.github.io/why-prototypal-inheritance-matters/#stop_using_the_new_keyword - 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? – Bergi Feb 10 '14 at 16:26
  • @Bergi I tend to dis agree as Aadit states himself; "you don't have to understand prototype to use it". It's like answering "use jQuery" to any question relating to DOM. Most of the article is about why not use new and mix in patterns do not accommodate for initialising instance members. The extend function doesn't actually allow you to extend parent functions (only override). Claims you can clone objects with Object.create. But fails to explain the behavior of the prototype chain and difference from instance to shared members. – HMR Feb 11 '14 at 01:17
  • @Bergi http://stackoverflow.com/questions/21669081/why-would-using-new-and-constructor-functions-be-bad under "The Object.create function can also be used to clone an existing object" you'll find that by using the wrong wording the reader may miss the point of the prototype and init/constructor function completely. The pattern is not wrong but the explanation about what's going on is. It starts looking like a rant now so will leave it at that. – HMR Feb 11 '14 at 02:10