Start with your module, utils.coffee:
exports.foo = ->
exports.bar = ->
Then your main file:
utils = require './utils'
utils.foo()
foo() and bar() are functions you'll be calling frequently, so you:
foo = require('./utils').foo
bar = require('./utils').bar
foo()
This approach works when only a few functions are defined in the module, but becomes messy as the number of functions increases. Is there a way to add all of a module's functions to your app's namespace?
Use extend
(with underscore or any other library that provides it. Write it yourself if necessary):
_(global).extend(require('./utils'))
If you don't want to use underscore, you could simply do:
var utils = require('./utils')
for (var key in utils)
global[key] = utils[key]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With