Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

module

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

Is there a pattern to do this? Do I just make a private function and wrap it?

Here's some example code:

(function() {
    "use strict";

    module.exports = function (params) {
        return {
            funcA: function() {
                console.log('funcA');
            },
            funcB: function() {
                funcA(); // ReferenceError: funcA is not defined
            }
        }
    }
}());
like image 218
andymurd Avatar asked Aug 23 '12 12:08

andymurd


1 Answers

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;
    };
}());
like image 50
Vadim Baryshev Avatar answered Nov 07 '22 19:11

Vadim Baryshev