title: Why does CoffeeScript wrap class definitions in a closure?
body: <p>In CoffeeScript, <a href="http://coffeescript.org/#try:thing1%20%3D%203%0A%0Aclass%20Foo%0A%20%20%40CONST%20%3D%2078%0A%20%20thing1%20%3D%2098%0A%20%20thing2%20%3D%2099%0A%0A%20%20constructor%20(a%2C%20b%2C%20c)%20-%3E%0A%20%20%20%20%40a%20%3D%20a%0A%20%20%20%20%40b%20%3D%20b%0A%20%20%20%20variable%20%3D%20c%0A%0A%20%20%40static_method%3A%20(x)%20-%3E%0A%20%20%20%20x%20*%202%0A%0A%20%20%40method%3A%20(x)%20-%3E%0A%20%20%20%20x%2B1%0A%0A" rel="noreferrer">this</a>:</p>
  
  <pre><code>class Foo
    method: (x) -&gt;
      x+1
  </code></pre>
  
  <p>Compiles to:</p>
  
  <pre><code>// Generated By CoffeeScript
  Foo = (function() {
    function Foo() {}
    Foo.prototype.method = function(x) {
      return x+1;
    }
    return Foo;
  })()
  </code></pre>
  
  <p>Which seems a bit excessive.  The following should be <em>functionally identical</em>:</p>
  
  <pre><code>// Generated by Dave
  function Foo() {}
  Foo.prototype.method = function(x) {
      return x+1;
  }
  </code></pre>
  
  <p><strong>What is the motivation for the extra "closure" wrapper?</strong></p>
  
  <p>This is not merely an idle question of styling; it has implication to overall code size.</p>
  
  <p>The Coffee version minifies into 84 bytes:</p>
  
  <pre><code>Foo=function(){function e(){}return e.prototype.method=function(e){return e+1},e}();
  </code></pre>
  
  <p>My version minifies into only 61 bytes:</p>
  
  <pre><code>function Foo(){}Foo.prototype.method=function(e){return e+1};
  </code></pre>
  
  <p><strike>23 bytes is silly kinds of irrelevant, but in a project with many many classes, overhead begins to add up.</strike></p>
  
  <p>Ok, I wrote an answer below refuting the byte size theory ... for any reasonable class, the Coffee method is going to be smaller.</p>
  
  <p>There's probably other reasons too.  Help me think of them.</p>