2014-03-06 59 views
1

如何在CoffeeScript中創建一個命名的函數表達式,如下面的例子?如何在CoffeeScript中創建一個命名的函數表達式?

var a = function b (param1) {} 

return function link (scope) {} 
+0

你爲什麼要這麼做? CoffeeScript不允許你使用它,而且它通常是不必要的(只需將該函數分配給一個變量)。 –

+0

[CoffeeScript中的函數聲明]的可能重複(http://stackoverflow.com/questions/6548750/function-declaration-in-coffeescript) –

回答

0

的CoffeeScript不支持後者(命名函數),但前者可以用

a = (param1) -> 
    console.log param1 
+2

這是一個匿名函數表達式。我想要一個命名函數,但你說coffeescript不支持它們。這有點垃圾。 – screenm0nkey

+0

這是一個(非常)舊的線程,但它解釋了他們採取的立場以及爲什麼他們仍然不允許在coffeescript中命名函數表達式:https://github.com/jashkenas/coffeescript/issues/15 – ynkr

4

有點晚了實現可能是我黨,但我只是意識到,您使用class關鍵字時實際上創建了命名函數。

例子:

class myFunction 
    # The functions actual code is wrapped in the constructor method 
    constructor: -> 
    console.log 'something' 

console.log myFunction # -> function AppComponent() { ... } 
myFunction() # -> 'something' 
相關問題