16

I saw a video in which Crockford told us not to use the new keyword. He said to use Object.create instead if I'm not mistaken. Why does he tell us not to use new if he has used it in achieving prototypal inheritance in this article that he wrote: http://javascript.crockford.com/prototypal.html

I would expect him to use Object.create instead of new, like this:

function object(o) {
    return Object.create((function() {}).prototype = o);
}

So why is it that he still uses new?

David G
  • 94,763
  • 41
  • 167
  • 253
  • 1
    Maybe he learned something since that video/article? It's hard to know without the video/context. – alexn Nov 20 '11 at 16:30
  • 2
    Probably a combination of A) `Object.create` not being [supported](http://kangax.github.com/es5-compat-table/) across a handful of browsers, and B) that article having been written several years ago; his programming style, like most of ours, has probably changed and evolved since then. – Rob Hruska Nov 20 '11 at 16:32
  • Here's the video: http://www.youtube.com/watch?v=hQVTIJBZook (I don't know exactly where in the video he said that, sorry) – David G Nov 20 '11 at 16:32
  • 1
    Also, in the article you reference, his addendum on 2008-04-07 essentially answers this, where he defines `Object.create` if it doesn't exist. You'd only have to use `new` that one time (defining `Object.create`). – Rob Hruska Nov 20 '11 at 16:40
  • Is that your code in the question, or did you find it somewhere? The reason I ask is that the `(function() {}).prototype = ` part of it has no effect on your code. You'll get the same result if you do `return Object.create( o );` – RightSaidFred Nov 20 '11 at 17:41
  • possible duplicate of [Is JavaScript 's "new" Keyword Considered Harmful?](http://stackoverflow.com/questions/383402/is-javascript-s-new-keyword-considered-harmful) – Bergi May 06 '13 at 17:36
  • 1
    As of November 2017, Douglas Crockford says he has stopped using `new`, `Object.create()`, _and_ `this`: https://youtu.be/DxnYQRuLX7Q?t=29m6s – ESV Jul 03 '18 at 00:26
  • @ESV Wow so much has changed since 2011. :) – David G Jul 03 '18 at 00:36

2 Answers2

7

Crockford discusses new and Object.create in this Nov 2008 message to the JSLint.com mailing list. An excerpt:

If you call a constructor function without the new prefix, instead of creating and initializing a new object, you will be damaging the global object. There is no compile time warning and no runtime warning. This is one of the language’s bad parts.

In my practice, I completely avoid use of new.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
Paul Beusterien
  • 27,542
  • 6
  • 83
  • 139
  • Maybe I am not familiar with all javascript hacks, but why would anyone call a constructor function without the `new` keyword?!? – xmashallax Jul 05 '13 at 22:21
  • 1
    What Crockford *actually* says is that if you *omit* the new keyword you will clobber the global object. It is the risk of omitting it which is the reason why he recommend not to use it. It is required for constructor objects. –  Jul 05 '13 at 23:32
5

This is a really old question, but…

When Crockford wrote the article you mention and the discussion linked by Paul Beusterien, there was no Object.create. It was 2008 and Object.create was, at most, a proposal (or really rarely implemented in browsers).

His final formulation is basically a polyfill:

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

newObject = Object.create(oldObject);

His only use of new is, therefore, to simulate (or emulate) what Object.create must do.

He doesn’t advise us to use it. He presents a polyfill that uses it, so you don’t have to use it anywhere else.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
slacktracer
  • 6,262
  • 6
  • 28
  • 33