Immediately invoked functions in CoffeeScript
NickName:morden.dk Ask DateTime:2012-02-16T15:39:11

Immediately invoked functions in CoffeeScript

Could someone show me how to immediately invoke a function in CoffeeScript. I'm trying to accomplish something similar to this JS object literal.

WEBAPP = {
    maxHeight : function(){
        /* Calc stuff n' stuff */
        WEBAPP.maxHeight = /* Calculated value */
    }(),
    someProperty : ''
    /* ... */        
}

Is it possible or what are the workarounds?

Copyright Notice:Content Author:「morden.dk」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/9307303/immediately-invoked-functions-in-coffeescript

Answers
Thilo 2012-02-16T07:52:17

There is do:\n\nWEBAPP = \n maxheight: do -> 1+1\n someProperty: ''\n\n\nWhich compiles to\n\nvar WEBAPP;\n\nWEBAPP = {\n maxheight: (function() {\n return 1 + 1;\n })(),\n someProperty: ''\n};\n",


XåpplI'-I0llwlg'I - 2014-06-05T15:21:36

For anyone else coming across this question, you can also combine the do keyword with default function parameters to seed recursive \"immediately-invoked functions\" with an initial value. Example:\n\ndo recursivelyPrint = (a=0) ->\n console.log a\n setTimeout (-> recursivelyPrint a + 1), 1000\n",


More about “Immediately invoked functions in CoffeeScript” related questions