How to return object explicitly in CoffeeScript

Alice picture Alice · Feb 5, 2011 · Viewed 21.1k times · Source

This works:

myfunc = () ->
    id: 3
    name: 'myname'

But I want to be explicit about returning object.

myfunc = () ->
    return
        id: 3
        name: 'myname'

But I get "Unexpected 'INDENT'" error. What's wrong with the above code?

Answer

matyr picture matyr · Feb 5, 2011
myFunc = ->
  return {
    id   : 3
    name : 'myname'
  }

myFunc = ->
  return {} =
    id   : 3
    name : 'myname'

myFunc = ->
  # return
  id   : 3
  name : 'myname'