Заметил сегодня один баг в CoffeeScript:
Foo = ->
@a = 1
this.method = () -> @
instance = new Foo
console.log typeof instance #function!
Транслируется в:
var Foo = function() {
this.a = 1;
return this.method = function() { //WTF?
return this;
};
};
Хотя по логке должно должно быть так:
var Foo = function() {
this.a = 1;
this.method = function() {
return this;
};
};
console.log(typeof instance) //object!
Т.е. без неявного добавления
return
Можно конечно переписать этот кусок так:
class Foo
constructor: ->
@a = 1
@method = -> this
instance = new Foo
console.log typeof instance #object!
Но итоговая трансляция более громоздкая:
var Foo;
Foo = (function() {
function Foo() {
this.a = 1;
this.method = function() {
return this;
};
}
return Foo;
})();
Мне одному кажется что неявное добавление инструкции
return ко всем последним выражениям в блоке это баг!?
