Why does coffeescript generate classes like this?
- by ryeguy
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?