Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3391392
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:47:55+00:00 2026-05-18T03:47:55+00:00

On Coffeescript.org: bawbag = (x, y) -> z = (x * y) bawbag(5, 10)

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:47:56+00:00Added an answer on May 18, 2026 at 3:47 am

    Since coffee script has no var statement it automatically inserts it for all variables in the coffee-script, that way it prevents the compiled JavaScript version from leaking everything into the global namespace.

    So since there’s no way to make something “leak” into the global namespace from the coffee-script side of things on purpose, you need to define your global variables as properties of the global object.

    attach them as properties on window

    This means you need to do something like window.foo = 'baz';, which handles the browser case, since there the global object is the window.

    Node.js

    In Node.js there’s no window object, instead there’s the exports object that gets passed into the wrapper that wraps the Node.js module (See: https://github.com/ry/node/blob/master/src/node.js#L321 ), so in Node.js what you would need to do is exports.foo = 'baz';.

    Now let us take a look at what it states in your quote from the docs:

    …targeting both CommonJS and the browser: root = exports ? this

    This is obviously coffee-script, so let’s take a look into what this actually compiles to:

    var root;
    root = (typeof exports !== "undefined" && exports !== null) ? exports : this;
    

    First it will check whether exports is defined, since trying to reference a non existent variable in JavaScript would otherwise yield an SyntaxError (except when it’s used with typeof)

    So if exports exists, which is the case in Node.js (or in a badly written WebSite…) root will point to exports, otherwise to this. So what’s this?

    (function() {...}).call(this);
    

    Using .call on a function will bind the this inside the function to the first parameter passed, in case of the browser this would now be the window object, in case of Node.js it would be the global context which is also available as the global object.

    But since you have the require function in Node.js, there’s no need to assign something to the global object in Node.js, instead you assign to the exports object which then gets returned by the require function.

    Coffee-Script

    After all that explanation, here’s what you need to do:

    root = exports ? this
    root.foo = -> 'Hello World'
    

    This will declare our function foo in the global namespace (whatever that happens to be).
    That’s all 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.