87

How to write this in coffeescript?

f = (function(){
   // something
})();

Thanks for any tips :)

Harmen
  • 22,092
  • 4
  • 54
  • 76
user537339
  • 1,851
  • 2
  • 15
  • 19

8 Answers8

159

While you can just use parentheses (e.g. (-> foo)(), you can avoid them by using the do keyword:

do f = -> console.log 'this runs right away'

The most common use of do is capturing variables in a loop. For instance,

for x in [1..3]
  do (x) ->
    setTimeout (-> console.log x), 1

Without the do, you'd just be printing the value of x after the loop 3 times.

Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
  • 24
    You can also write `f = do -> console.log x` – scribu Aug 07 '11 at 16:26
  • 5
    @scribu Well, those two statements are very different, and yours is actually the one that I should have given. Mine assigns the function `-> console.log 'this runs right away'` to `f`, then runs it; yours runs the function and then assigns its result to `f`, as in the original question. (Though in the case of `console.log`, the return value is always `undefined` anyway.) – Trevor Burnham Aug 07 '11 at 19:23
  • 1
    Exactly. Also, you can define object properties this way: `{f: do -> // something}` – scribu Aug 07 '11 at 20:31
  • 2
    As of CoffeeScript 1.3.1 (released April 2012), `do` also lets you pass arguments to the function. To pass `1` and `2` as the parameters `x` and `y`, write `do (x = 1, y = 2) ->`. (The documentation for this feature has [gotten lost](https://github.com/jashkenas/coffee-script/pull/2660#issuecomment-14369711), but [the issue where the feature was introduced](https://github.com/jashkenas/coffee-script/issues/960) has some examples.) – Rory O'Kane Sep 10 '13 at 20:24
19

If you want to "alias" the arguments passed to self-invoking function in CoffeeScript, and let's say this is what you are trying to achieve:

(function ( global, doc ) {
  // your code in local scope goes here
})( window, document );

Then do (window, document) -> won't let you do that. The way to go is with parens then:

(( global, doc ) -> 
  # your code here
)( window, document ) 
Misha Reyzlin
  • 13,736
  • 4
  • 54
  • 63
17

it's ridiculous easy in coffee:

do ->

will return

(function() {})();
mart7ini
  • 1,519
  • 2
  • 16
  • 21
6

try to use

do ($ = jQuery) ->
Pavel Blagodov
  • 572
  • 5
  • 6
5

You can also combine the do keyword with default function parameters to seed recursive "self-initiating functions" with an initial value. Example:

do recursivelyPrint = (a=0) ->
  console.log a
  setTimeout (-> recursivelyPrint a + 1), 1000
XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151
3
do ->
    #your stuff here

This will create a self executing closure, which is useful for scoping.

  • 1
    This doesn't seem to add a whole lot of new information compared to the other answers already posted. Rather than creating a competing answer, you might add more value by posting an informative comment under another answer that adds some clarity or useful information to the other answer. – still_dreaming_1 Feb 05 '15 at 22:37
1

Apologies, I solved it:

f = (
    () -> "something"
)()
user537339
  • 1,851
  • 2
  • 15
  • 19
  • 1
    @Squeegy I wouldn't necessarily say that. The `do` keyword has some limitations that make it necessary to use the JS-style approach sometimes (see [issue 960](https://github.com/jashkenas/coffee-script/issues/960)); `do` was really only added because of the loop-with-closures use case. – Trevor Burnham Aug 08 '11 at 13:42
  • 1
    But we aren't using CoffeeScript to punch ourselves in the face like that either. – Brandon Aug 31 '11 at 20:10
0

It should be

f = () ->
  # do something
Alongkorn
  • 3,968
  • 1
  • 24
  • 41