-3

I want to export a function, call it someFunction:

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

module.exports.someFunction = someFunction

but I am thinking about encapsulating it in another function

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

What is the correct way to export it using modules?

lwang135
  • 576
  • 1
  • 3
  • 13
fox
  • 15,428
  • 20
  • 55
  • 85

1 Answers1

0

Did you mean something like:

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

This can be called via:

var test = require('./test'); // file with function

var func = test();
func(a); // console.log(a);

Is that how you wanted to accomplish this?

lwang135
  • 576
  • 1
  • 3
  • 13
  • thanks but no I was hoping to encapsulate someplace other than the exports context. – fox Dec 12 '14 at 23:05
  • @fox I do not understand either. You need to export _something_ in order to access something. Either `exports` directly or through a function. – lwang135 Dec 12 '14 at 23:09
  • Sorry, my response may have been vague. What I am looking to do is to export a set of inner functions that I can encapsulate in an outer function, as in my example above. I want to preserve the hierarchy between `someFunction` and `someOtherFunction` in my code, but only export `someFunction`. – fox Dec 12 '14 at 23:22
  • @fox `someOtherFunction` could possibly be [used as a closure](http://stackoverflow.com/q/111102/), maybe as an [IIFE](http://stackoverflow.com/q/8228281/), with `someFunction` being [revealed from it](http://stackoverflow.com/q/5647258/). Though, with [Node.js' module scope](http://stackoverflow.com/q/19850234/), such a closure already exists around the entire `.js` file with `module.exports` for revealing. – Jonathan Lonowski Dec 12 '14 at 23:50