Node.js: calling one exported function from another in the same module

I am writing a node.js module that exports two functions, and I want to call one function from another, but I see an undefined link error.

Is there a template for this? Am I just doing a private function and wrapping it?

Here is a sample code:

(function() { "use strict"; module.exports = function (params) { return { funcA: function() { console.log('funcA'); }, funcB: function() { funcA(); // ReferenceError: funcA is not defined } } } }()); 
+6
source share
1 answer

I like this way:

 (function() { "use strict"; module.exports = function (params) { var methods = {}; methods.funcA = function() { console.log('funcA'); }; methods.funcB = function() { methods.funcA(); }; return methods; }; }()); 
+8
source

Source: https://habr.com/ru/post/923552/


All Articles