1

How do I create a named function expressions in CoffeeScript like the examples below?

var a = function b (param1) {}

or

return function link (scope) {}
screenm0nkey
  • 18,405
  • 17
  • 57
  • 75
  • Why do you want to do that? CoffeeScript doesn't allow you to, and its typically unnecessary (just assign the function to a variable). – Aaron Dufour Mar 06 '14 at 15:50
  • possible duplicate of [Function declaration in CoffeeScript](http://stackoverflow.com/questions/6548750/function-declaration-in-coffeescript) – mu is too short Mar 06 '14 at 17:55

2 Answers2

5

I may be a bit late to the party, but I just realised that you actually create named functions when using the class keyword.

Example:

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

console.log myFunction # -> function AppComponent() { ... }
myFunction() # -> 'something'
naeramarth7
  • 6,030
  • 1
  • 22
  • 26
0

Coffeescript doesn't support the latter (named functions), but the former can be achieved with

a = (param1) ->
    console.log param1
spikeheap
  • 3,827
  • 1
  • 32
  • 47
  • 3
    That's an anonymous function expression. I wanted a named function but you say coffeescript doesn't support them. that's a bit rubbish. – screenm0nkey Mar 06 '14 at 21:10
  • Here is a (very) old thread, but it explains the stance they took and why they still do not allow named function expressions in coffeescript: https://github.com/jashkenas/coffeescript/issues/15 – ynkr Jul 14 '14 at 06:15