如何在咖啡脚本中写这个?
f = (function(){
   // something
})();
感谢您的任何提示:)
如何在咖啡脚本中写这个?
f = (function(){
   // something
})();
感谢您的任何提示:)
虽然你可以只使用括号(例如,你可以通过使用关键字(-> foo)()来避免它们:do
do f = -> console.log 'this runs right away'
最常见的用途do是在循环中捕获变量。例如,
for x in [1..3]
  do (x) ->
    setTimeout (-> console.log x), 1
如果没有do,您只会x在循环后打印 3 次的值。
如果您想“别名”传递给 CoffeeScript 中的自调用函数的参数,假设这就是您要实现的目标:
(function ( global, doc ) {
  // your code in local scope goes here
})( window, document );
然后do (window, document) ->不会让你这样做。那么要走的路是使用parens:
(( global, doc ) -> 
  # your code here
)( window, document ) 
    喝咖啡很容易:
do ->
将返回
(function() {})();
    尝试使用
do ($ = jQuery) ->
    您还可以将do关键字与默认函数参数结合起来,为具有初始值的递归“自启动函数”提供种子。例子:
do recursivelyPrint = (a=0) ->
  console.log a
  setTimeout (-> recursivelyPrint a + 1), 1000
    do ->
    #your stuff here
这将创建一个自执行闭包,这对于范围界定很有用。
抱歉,我解决了:
f = (
    () -> "something"
)()
    它应该是
f = () ->
  # do something