23 avr. 2014

Global var in CoffeeScript for Meteor

Just a reminder as I've struggled a bit on it. Meteor.js uses a nice exposing feature of global vars in packages. On the other hand, CoffeeScript forbids the use on inexplicit variable by creating an anonymous namespace, therefore preventing global scope pollution.

OK... but how do we marry back this to approach. Sugar your coffee provides a nice answer:

  • In Javascript:
    myglobal = new Global('something');
  • In Coffeescript (use the this operator: the @):
    @myglobal = new Global 'something'