我正在编写一个node.js模块,它导出两个函数,我想从另一个函数调用一个函数,但我看到一个未定义的引用错误.
有这样的模式吗?我只是创建一个私有函数并将其包装好吗?
这是一些示例代码:
- (function() {
- "use strict";
- module.exports = function (params) {
- return {
- funcA: function() {
- console.log('funcA');
- },funcB: function() {
- funcA(); // ReferenceError: funcA is not defined
- }
- }
- }
- }());
解决方法
我喜欢这样:
- (function() {
- "use strict";
- module.exports = function (params) {
- var methods = {};
- methods.funcA = function() {
- console.log('funcA');
- };
- methods.funcB = function() {
- methods.funcA();
- };
- return methods;
- };
- }());