找不到undefined的功能 - CommonJS模式

the*_*ava 0 javascript module node.js

var revealed = function(){
   var a = [1,2,3];
   function abc(){
     return (a[0]*a[1])+a[2]);
   }

   return {
      name: 'revealed',
      abcfn: abc
   }

   module.exports = revealed;
}();
Run Code Online (Sandbox Code Playgroud)

如何将CommonJS模式与Revealing Module模式一起使用?当我尝试module.exports = revealed;并尝试将模块包含在某个地方来调用该函数时,它会引发错误说法function not found.

var module = require('module');
module.abc();
Run Code Online (Sandbox Code Playgroud)

无法调用未定义的方法.

phe*_*nal 5

在分配给module.exports之前,您将返回该对象!

你想要的是这个:

var revealed = function(){
   var a = [1,2,3];
   function abc(){
     return (a[0]*a[1])+a[2]);
   }

   return {
      name: 'revealed',
      abcfn: abc
   }
}();

module.exports = revealed;
Run Code Online (Sandbox Code Playgroud)