For code reuse purpose I want to capture some logic in a single function and have it called in other modules
Here is the function definition
// Module A
define (require) ->
doSomething(a, b, c) ->
"#{a}?#{b}&#{c}"
And here is how the funciton doSomething
is used
// Module B
define(require) ->
a = require 'A'
...
class Bee
constructor: ->
@val = a.doSomething(1, 2, 3)
However in the browser, I got this error message
Uncaught ReferenceError: doSomething is not defined
What is the proper way to export/import a free function in coffeescript?
This:
isn't a function definition. That is really this in disguise:
so your module is trying to call the
doSomething
function and then call what it returns as another function which takes a third function as an argument. Then whateverdoSomething(...)(...)
returns is sent back from the module.So when you say this:
you're getting "who knows what" in
a
and that thing doesn't have adoSomething
property soa.doSomething(1,2,3)
gives you aReferenceError
.I think you want to wrap your function in an object in your module:
Alternatively, you could just return the function:
and then use it like this: