How do I define global variables in CoffeeScript?

On Coffeescript.org:

bawbag = (x, y) ->
    z = (x * y)

bawbag(5, 10) 

would compile to:

var bawbag;
bawbag = function(x, y) {
  var z;
  return (z = (x * y));
};
bawbag(5, 10);

compiling via coffee-script under node.js wraps that so:

(function() {
  var bawbag;
  bawbag = function(x, y) {
    var z;
    return (z = (x * y));
  };
  bawbag(5, 10);
}).call(this);

Docs say:

If you’d like to create top-level variables for other scripts to use,
attach them as properties on window, or on the exports object in
CommonJS. The existential operator (covered below), gives you a
reliable way to figure out where to add them, if you’re targeting both
CommonJS and the browser: root = exports ? this

How do I define Global Variables then in CoffeeScript. What does ‘attach them as properties on window’ mean?

9 Answers
9

Leave a Comment