为什么coffeescript到处使用"返回"声明?

m4t*_*m4t 3 javascript coffeescript

写这样的东西时:

$(document).ready ->
  doSomething()

doSomething = ->
  alert('Nothing to do')
Run Code Online (Sandbox Code Playgroud)

编译成

$(document).ready(function() {
  return doSomething();
});

doSomething = function() {
  return alert('Nothing to do');
};
Run Code Online (Sandbox Code Playgroud)

据我所知,return语句用于值(字符串,数组,整数......)

为什么coffeescript会这样做?

Dav*_*ton 8

如果没有指定,CoffeeScript使用隐式返回.

CS返回函数中最后一个语句的值.这意味着生成的JS将具有return最后一个语句的值,因为JS需要显式return.

return语句用于值(字符串,数组,整数......)

是的,并且可以通过调用函数来返回这些值,例如doSomething()alert()在您的示例中.值是执行方法的结果是无关紧要的.