Is "new" not yet recommended in JavaScript?

So, I see a lot of JavaScript code that uses the β€œnew” when creating constructors. After reading the β€œGood Details” part of JavaScript, it seems like using the β€œnew” is not cat pajamas. That was 4 years ago, though ... Isn't that recommended yet? What is the current standard?

+6
source share
2 answers

Since when is new not recommended? D. Crockford has the right point of view and strong opinion, but new is part of the language, and it is very used in many projects. new is part of the prototype inheritance model and should be used to create new instances using the constructor function. Crockford points to a purely functional approach, using the this and return this contexts to be able to inherit properties and methods between child objects. This is another solution to the general problem, but that does not mean that new should not be used. In fact, one of the most copied / pasted JS snippets of all time is Crockford's, object.create polyfill, and it uses new :

 if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; return new F(); }; } 
+7
source

Nothing has changed since 2008, except that Object.create , affected by Crockford, made its way into ECMAScript 5: new still has the same behavior and flaws that Douglas Crockford strongly pointed out. My own opinion is that he rather overestimated the problems and turned the developers against a useful operator, so I continue to use it without problems; I would suggest other developers make their own mind, rather than blindly internalize and spew out (admittedly wonderful) Crockford.

+2
source

Source: https://habr.com/ru/post/922446/


All Articles