Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在NodeJS的一个模块中导出多个函数_Javascript_Node.js - Fatal编程技术网

Javascript 在NodeJS的一个模块中导出多个函数

Javascript 在NodeJS的一个模块中导出多个函数,javascript,node.js,Javascript,Node.js,嗨,我在尝试导出一个包含两个函数的模块时,在Nodejs(使用express)中遇到了一个问题,其结构如下 exports.class1 = function(){ return = { method1 : function(arg1, arg2){ ........... }, method2 : function (arg2, arg3, arg4){ ..........

嗨,我在尝试导出一个包含两个函数的模块时,在Nodejs(使用express)中遇到了一个问题,其结构如下

exports.class1 = function(){
    return = {
         method1 : function(arg1, arg2){
             ...........
         },
         method2 : function (arg2, arg3, arg4){
             ..........  
         }

   };

 }
此模块另存为module1.js 当它被导入并按如下方式使用时,会发生错误

var module1 = require('./module1');

module1.class1.method1(arg1, arg2);

你的
class1
应该有如下代码

exports.class1 = function(){
    return {
         method1 : function(arg1, arg2){
             console.log(arg1,arg2)
         },
         method2 : function (arg2, arg3, arg4){
             console.log(arg2,arg3,arg4);
         }

   };

 }
module1.class1().method1(arg1, arg2);//because class1 is a function
你必须像贝娄一样称呼它

exports.class1 = function(){
    return {
         method1 : function(arg1, arg2){
             console.log(arg1,arg2)
         },
         method2 : function (arg2, arg3, arg4){
             console.log(arg2,arg3,arg4);
         }

   };

 }
module1.class1().method1(arg1, arg2);//because class1 is a function
更好的方法是导出对象

  exports.class1 = {
         method1 : function(arg1, arg2){
             console.log(arg1,arg2)
         },
         method2 : function (arg2, arg3, arg4){
             console.log(arg2,arg3,arg4);
         }
 }
你可以这样称呼它

module1.class1.method1(arg1, arg2); //because here class1 is an object

这是什么错误?为什么要导出函数?导出对象。您的
返回
语法不正确…您能纠正返回语法吗?添加了一个答案,给出了导出
函数
对象
。当将其用作对象时,是否可以在method2中调用method1???@ShenalSilva不在这种情况下,那么您必须执行类似的操作,请参见此处