Why does coffeescript generate classes like this?

Posted by ryeguy on Stack Overflow See other posts from Stack Overflow or by ryeguy
Published on 2011-01-12T16:16:36Z Indexed on 2011/01/12 17:53 UTC
Read the original article Hit count: 157

Filed under:
|

Given the following coffeescript code:

class Animal
  constructor: (@name) ->
  speak: (things) -> "My name is #{@name} and I like #{things}"

This is generated:

var Animal = (function() {
  function Animal(name) {
    this.name = name;
  }
  Animal.prototype.speak = function(things) {
    return "My name is " + this.name + " and I like " + things;
  };
  return Animal;
})();

But why isn't this more idiomatic code generated?

var Animal = function Animal(name) {
  this.name = name;
};
Animal.prototype.speak = function(things) {
  return "My name is " + this.name + " and I like " + things;
};

I know that coffeescript wraps a lot of stuff in anonymous functions to control scope leak, but what could leak here?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about coffeescript