Loading ...
Sorry, an error occurred while loading the content.

596Re: new function ()

Expand Messages
  • Douglas Crockford
    Jun 2, 2009
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@...> wrote:
      >
      > I am curious why JSLint dislikes this:
      >
      > var foo = new function () {
      > this.bar = 1;
      > }();
      >
      > Without the "new", "this" is bound to the global object. It is true that this is a rather odd form that I doubt is seen very often, nonetheless I don't see anything inherently wrong with it.
      >
      > Mind explaining?

      The object will always have the new function's prototype in its
      inheritance chain, which will be linked to the function. Every
      time you make another foo, you are going to make another function
      object and prototype object, and prevent them from being garbage
      collected.

      There's no point to that, particularly when you have better forms like

      var foo = (function () {
      return {
      bar: 1;
      };
      }());

      or even better

      var foo = {
      bar: 1
      };
    • Show all 3 messages in this topic