0

How do I use CoffeeScript to call a function with multiple functions as arguments? For example, if I wanted to compile to this JavaScript,

outerFunction(function(innerFunction1) {
        console.log('argument 1');
    },
    function(innerFunction2) {
        console.log('argument 2');
    },
    argument3
);

what CoffeeScript code would I use? Assume indents of 2 spaces, even though I used 4 to make it look better on StackOverflow. To be honest, getting the JavaScript indentation right is hard, too. Regardless, I have tried

outerFunction (innerFunction1) ->
    console.log 'argument 1'
(innerFunction2) ->
    console.log 'argument 2'
argument3

but the resulting JavaScript had innerFunction2 be at the same level as outerFunction, rather than as a child. Using commas did not seem to help, either. Having both functions on the same line like this:

outerFunction (innerFunction1) ->
        console.log 'argument 1', (innerFunction2) ->
        console.log 'argument2,
    argument3

has the opposite problem: innerFunction2 becomes a child of innerFunction1.

So how do I make multiple functions be arguments of a single function in CoffeeScript?

trysis
  • 8,086
  • 17
  • 51
  • 80
  • I didn't see that question, probably because I didn't think of them as "anonymous functions". Regardless, no offense, but that isn't a very good question and doesn't have many good answers. – trysis Nov 23 '14 at 22:42

2 Answers2

2

Here is what you want:

outerFunction (innerFunction1) ->
  console.log 'argument 1'
, (innerFunction2) ->
  console.log 'argument 2'
, argument3

Unfortunately, you can't write it in CoffeeScript without using either commas or parentheses:

outerFunction(
  (innerFunction1) ->
    console.log 'argument 1'
  (innerFunction2) ->
    console.log 'argument 2'
  argument3
)

Unless you have a first argument which is not an anonymous function

outerFunction argument1,
  (innerFunction2) ->
    console.log 'argument 2'
  (innerFunction3) ->
    console.log 'argument 3'
  argument4
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
  • Thank you. I do not like comma-first programming, but I suppose I'll have to get used to it if I want to keep writing `CoffeeScript`. – trysis Nov 23 '14 at 21:43
1

I suspect you are mixing spaces with indents, or your indents are off. Because the CoffeeScript you posted becomes fine JavaScript.

outerFunction ->
    innerFunction1 ->
        console.log 'argument 1'
    innerFunction2 ->
        console.log 'argument 2'
    argument3

Compiles to:

outerFunction(function() {
  innerFunction1(function() {
    return console.log('argument 1');
  });
  innerFunction2(function() {
    return console.log('argument 2');
  });
  return argument3;
});

its also fairly hard to know what you want, because your example:

outerFunction(function() {
    innerFunction1() {
        console.log('argument 1');
    },
    innerFunction2() {
        console.log('argument 2');
    },
    argument3
);

is not valid JavaScript

15 warnings

  • 2 Missing semicolon.
  • 4 Expected an identifier and instead saw ','.
  • 4 Expected an assignment or function call and instead saw an expression.
  • 4 Missing semicolon.
  • 5 Missing semicolon.
  • 7 Expected an identifier and instead saw ','.
  • 7 Expected an assignment or function call and instead saw an expression.
  • 7 Missing semicolon.
  • 8 Expected an assignment or function call and instead saw an expression.
  • 8 Missing semicolon.
  • 9 Expected an identifier and instead saw ')'.
  • 9 Expected an assignment or function call and instead saw an expression.
  • 1 Unmatched '{'.
  • 9 Expected ')' and instead saw ''.
  • 9 Missing semicolon.

Four undefined variables

  • 1 outerFunction
  • 2 innerFunction1
  • 5 innerFunction2
  • 8 argument3
Fresheyeball
  • 29,567
  • 20
  • 102
  • 164
  • You are right, the JavaScript I was trying to use was different from that in the question. I corrected it, but I may need to tweak it a little more. – trysis Nov 23 '14 at 20:41
  • My JavaScript isn't perfect, either, but your linter has some false positives. My JavaScript probably confused it. – trysis Nov 23 '14 at 20:46