我一直在使用咖啡脚本类,据我所知,函数应该总是自动返回最后一条语句。但是,我发现构造函数对象中定义的函数默认情况下不返回任何内容。为什么是这样?
咖啡脚本
constructor: ->
@ # <~~ returned as expected
class MyClass
constructor: ->
@ # <~~ not returned - why?
class MyClass
constructor: ->
return ->
@ # <~~ returned like normal!
JavaScript
var MyClass;
({
constructor: function() {
return this;
}
});
MyClass = (function() {
function MyClass() {
this;
}
return MyClass;
})();
MyClass = (function() {
function MyClass() {
return function() {
return this;
};
}
return MyClass;
})();