Coffeescript 如何在coffeesricpt中将函数从一个模块导出到另一个模块?

Coffeescript 如何在coffeesricpt中将函数从一个模块导出到另一个模块?,coffeescript,amd,Coffeescript,Amd,出于代码重用的目的,我想在单个函数中捕获一些逻辑,并在其他模块中调用它 下面是函数定义 // Module A define (require) -> doSomething(a, b, c) -> "#{a}?#{b}&#{c}" 下面是如何使用函数doSomething // Module B define(require) -> a = require 'A' ... class Bee constructor

出于代码重用的目的,我想在单个函数中捕获一些逻辑,并在其他模块中调用它

下面是函数定义

// Module A
define (require) ->

  doSomething(a, b, c) ->
    "#{a}?#{b}&#{c}"
下面是如何使用函数
doSomething

// Module B

define(require) ->

   a = require 'A'

...

   class Bee
     constructor: ->
       @val = a.doSomething(1, 2, 3)
但在浏览器中,我收到了此错误消息

Uncaught ReferenceError: doSomething is not defined
在coffeescript中导出/导入免费函数的正确方法是什么?

此:

define (require) ->

  doSomething(a, b, c) ->
    "#{a}?#{b}&#{c}"
不是函数定义。这实际上是伪装的:

define (require) ->
  return doSomething(a, b, c)( -> "#{a}?#{b}&#{c}")
因此,您的模块试图调用
doSomething
函数,然后调用它作为另一个函数返回的内容,该函数将第三个函数作为参数。然后从模块返回
doSomething(…)(…)
返回的任何内容

所以当你这么说的时候:

a = require 'A'
你在
a
中得到了“谁知道什么”,而这个东西没有
doSomething
属性,所以
a.doSomething(1,2,3)
会给你一个
引用错误

我认为您希望将函数包装在模块中的对象中:

define (require) ->
  doSomething: (a, b, c) ->
    "#{a}?#{b}&#{c}"
或者,您可以只返回函数:

define (require) ->
  (a, b, c) ->
    "#{a}?#{b}&#{c}"
然后像这样使用它:

doSomething = require 'A'
doSomething(1, 2, 3)

事实证明,只有最后一种样式(“只返回函数”)适合我