Using RequireJS exports with CoffeeScript

If you are using the “exports” object in your RequireJS module definition and happen to also be using CoffeeScript then be careful to either return the exports object at the end of the function or explicitly return nothing from the function by typing “return” on the last line. Like so:

define((require, exports, module) ->
    exports.foo = 'bar'
    return
)

If you don’t return nothing or the exports object CoffeeScript will compile this:

define((require, exports, module) ->
    exports.foo = 'bar'
)

…to the following javascript:

define(function(require, exports, module) {
    return exports.foo = 'bar';
});

Where the return value will be the string ‘bar’. This is no good because if you return a value from your module definition function, RequireJs uses it instead of the exports object. So when you come to require your module, you’ll be passed a String, rather than an object with a “foo” property as you’d expect.

17,846 thoughts on “Using RequireJS exports with CoffeeScript

  1. NewULife Ranks. Welcome to my NewUlife Review.

    Also visit my website; commercial flooring companies in phoenix
    (Brodie)

  2. USP human development hormone (somatropin).

    Feel free to visit my web blog :: commercial
    flooring near me prices (Jim)

  3. Эта публикация завернет вас в вихрь увлекательного контента, сбрасывая стереотипы и открывая двери к новым идеям. Каждый абзац станет для вас открытием, полным ярких примеров и впечатляющих достижений. Подготовьтесь быть вовлеченными и удивленными каждый раз, когда продолжите читать.
    Прочитать подробнее – выведение из запоя цена

  4. Hi, i think that i saw you visited my site thus i came to “return the favor”.I
    am attempting to find things to improve my website!I suppose its ok to
    use some of your ideas!!

  5. I think what you said was actually very reasonable. However, what about this?

    suppose you composed a catchier title? I ain’t saying your content
    isn’t solid., but suppose you added a post title that makes
    people want more? I mean Using RequireJS exports with
    CoffeeScript | freestyle developments is kinda vanilla.
    You might peek at Yahoo’s home page and see how they create post titles to get people to click.
    You might add a related video or a pic or two to grab people excited about what you’ve got
    to say. Just my opinion, it could bring your blog a little livelier.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>