-1

CoffeScript compiles this:

class A
  a: 'value'

to:

var A;

A = (function() {
  function A() {}

  A.prototype.a = 'value';

  return A;

})();

What is the difference with this:

var A = function A(){};
A.prototype.a = 'value';

I tested the codes in console and the first returns function A(), while the second returns "value", but as a class is intended to be instantiated, to use class A, myA = new A() works for both cases.

sites
  • 21,417
  • 17
  • 87
  • 146
  • Both are the same. The first just places an enclosing scope to the entire definition of `A` and it's members. – Joseph Apr 21 '13 at 01:21
  • 1
    @JosephtheDreamer: There are [things that you can do with the first](http://coffeescript.org/#try:class%20C%0A%20%20%20%20f%20%3D%20-%3E) that the second doesn't cover. – mu is too short Apr 21 '13 at 03:09

1 Answers1

0

There's no effective difference, but since CoffeeScript is a code generator, it likely has other uses for the variable scope in different situations, and is simply not optimized to reduce the code for the simple situations that don't actually need the extra scope.

I don't use CoffeeScript, but that would be my guess.