导出 node.js / coffeescript 中的私有函数

exporting a private function in node.js / coffeescript

本文关键字:函数 coffeescript node js 导出      更新时间:2023-09-26

我想导出一个函数,叫它someFunction:

someFunction = (foo)->
   console.log(foo)
module.exports.someFunction = someFunction

但我正在考虑将其封装在另一个函数中

someOtherFunction = ()->
    someFunction = (foo)->
        console.log(foo)

使用modules导出它的正确方法是什么?

你的意思是:

module.exports = function() {
  return function(a) {
     //encapsulated
     console.log(a);
  };
};

这可以通过以下方式称为:

var test = require('./test'); // file with function
var func = test();
func(a); // console.log(a);

这就是你想实现的方式吗?