为什么在 coffeescript 中每个块的末尾都需要一个 "undefined"?

Why would you need a "undefined" at the end of every block in coffeescript?

我正在处理另一个团队最初开发的非常大的 coffeescript 文件。出于某种原因,文件中几乎每个代码块的末尾都有一个 "undefined" 。例如:

  someFunction = () ->
    ajaxReq(
      blah blah
      undefined
    )
    undefined

 $("#some_id").click((e)->
    e.preventDefault()
    blah blah
    undefined
  )

   $(".some_class").change(->
     blah blah
     undefined
   )

谁能帮我理解他们为什么这样做,是否有必要?谢谢。

因为 CoffeeScript return 是函数的最后一个表达式。在JavaScript中,所有函数默认为returnundefined。在这些情况下你不必写 undefined 因为你没有使用 return 值但是如果你想要它保持一致,你必须 return undefined.