Node.js:在同一模块中从另一个调用一个导出函数

时间:2022-03-17 15:58:57

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.

我正在编写一个node.js模块,它导出两个函数,我想从另一个函数调用一个函数,但我看到一个未定义的引用错误。

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
            }
        }
    }
}());

1 个解决方案

#1


8  

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;
    };
}());

#1


8  

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;
    };
}());