How to pass two anonymous functions as arguments in CoffeScript?

I want to pass two anonymous functions as arguments to jQuery hover, for example:

$('element').hover( function() { // do stuff on mouseover }, function() { // do stuff on mouseout } ); 

Easy with one - hover -> - but what is the correct syntax in CoffeeScript for two? I tried ...hover -> , ...hover( ->... etc., but I was not able to get the above structure.

+55
javascript jquery anonymous-function coffeescript
Jun 24 2018-11-11T00:
source share
4 answers

Put parentheses around anonymous functions.

+24
Jun 24 2018-11-11T00:
source share

I think the problem is using single line // comments. The single-line comments enclosed in /* .. */ seem to work fine. Here is an equivalent example with something other than a comment.

 $('element').hover( -> console.log("first") -> console.log("second") ) 

Or with comments using /* .. */ .

 $('element').hover( -> /* first */ -> /* second */ ) 

You can try these examples on the Try CoffeeScript tab. CoffeeScript adds a return statement to return the last expression of the function. If you need bare-bones functions that do nothing and do not contain return at the end, try:

 $('element').hover( () -> () -> ) // $('element').hover(function() {}, function() {}); 
+52
Jun 25 '11 at 2:21
source share

Another way is to use a backslash after the call function, the comma must be indented correctly.

 $('element').hover \ -> # do stuff on mouseover , -> # do stuff on mouseout 
+21
Apr 22 2018-12-23T00:
source share

Without brackets or backslash :

 f -> 0 , -> 1 

Output to 1.7.1:

 f(function() { return 0; }, function() { return 1; }); 
+8
Jun 23 '14 at 17:43
source share



All Articles