Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immediately invoked functions in CoffeeScript

Tags:

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?

like image 697
morden.dk Avatar asked Feb 16 '12 07:02

morden.dk


1 Answers

There is do:

WEBAPP = 
  maxheight: do -> 1+1
  someProperty: ''

Which compiles to

var WEBAPP;

WEBAPP = {
  maxheight: (function() {
    return 1 + 1;
  })(),
  someProperty: ''
};
like image 148
Thilo Avatar answered Nov 15 '22 23:11

Thilo