Search code examples
functioncoffeescriptchaining

Chain function call after function definition


How can I chain a function call after a function definition in CoffeeScript?

Equivalent javascript would be:

var foo = function () {
    // stuff
}.bar()

The only way I managed to do it is:

foo = `function () {
    // stuff
}.bar()`

But I hope for a better solution than embedding javascript in my (beautiful) coffeescript code


Solution

  • Try like this:

    foo = (-> stuff).bar()
    

    For example:

    square = ((x)-> x*x).bar()
    

    Compiles into:

    var square;
    square = (function(x) {
      return x * x;
    }).bar();