0

im using the following code in a node application and I got error when I call to _valdations function, I want that _vali will be "private" like (I know that this is not supported native with JS,what is the recomended way to do so ? the vali function should be not exposed outside (just use for internal ...)

module.exports = {


        fileAction: function (req, res, urlPath) {

           ....
                  _validations(config, req, res);




        },

        _vali: function (config, req, res) {

          do some validations
        },

    };

2 Answers2

1

Don't export it. Just use it as a local variable.

function fileAction(etc) {

}

function vali(etc) {

}

module.exports = {
    fileAction: fileAction
    // vali: vali // Not exported
};
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks Quentin voted up !it appear that there is two (like the other answer) options to do that (there is cons and pros for each usage ? –  Jul 01 '15 at 10:45
  • Named functions are easier to deal with in debuggers (since they'll tell you the function name in the stack trace). – Quentin Jul 01 '15 at 10:47
  • Thanks,just to verify so this is the only difference ? –  Jul 01 '15 at 13:57
  • It's the only significant one in this case. http://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip – Quentin Jul 01 '15 at 14:05
1

Just don't add it to the module.exports:

var _vali = function (config, req, res) {
    // do some validations
}

module.exports = {
   fileAction: function (req, res, urlPath) {
      _vali(config, req, res);
    }
};
Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • Thanks Constantinius voted up !it appear that there is two (like the other answer) options to do that (there is cons and pros for each usage ? or this is just semantic difference –  Jul 01 '15 at 10:45