culebrón culebrón - 4 months ago 11
CoffeeScript Question

What's an elegant way to export objects in CoffeeScript module?

I have several functions in a CoffeeScript module:

func1 = () -> ...
func2 = () -> ...
func3 = () -> ...
func4 = () -> ...


If I want to make it clear where they come from (without searching for a definition), I'd avoid making them global (
@func1 = ...
,
@func2 = ...
) , and stick to a more explicit syntax:

helpers = require('/lib/helpers.coffee')


But this requires something like

meteor.exports.func1 = func1


repeated every time. Or

meteor.exports.func1 = () -> ...


But this way it's harder to make calls between them here inside.

I know ES6 has an elegant syntax like
{var1, var2, ...}
, but is there something similar in CoffeeScript?

Answer
func1 = () ->
func2 = () ->
module.exports = {func1, func2}

compiled to:

var func1, func2;
func1 = function() {};
func2 = function() {};
module.exports = {
  func1: func1,
  func2: func2
};
Comments