She*_*lva 2 javascript node.js
嗨我在尝试导出具有两个函数的模块时遇到了Nodejs(带快速)的问题,其结构如下
exports.class1 = function(){
return = {
method1 : function(arg1, arg2){
...........
},
method2 : function (arg2, arg3, arg4){
..........
}
};
}
Run Code Online (Sandbox Code Playgroud)
导入时此模块以module1.js形式保存,如下所示使用时会发生错误
var module1 = require('./module1');
module1.class1.method1(arg1, arg2);
Run Code Online (Sandbox Code Playgroud)
你class1应该有像下面这样的代码
exports.class1 = function(){
return {
method1 : function(arg1, arg2){
console.log(arg1,arg2)
},
method2 : function (arg2, arg3, arg4){
console.log(arg2,arg3,arg4);
}
};
}
Run Code Online (Sandbox Code Playgroud)
你必须像下面这样称呼它
module1.class1().method1(arg1, arg2);//because class1 is a function
Run Code Online (Sandbox Code Playgroud)
一种更好的方法来导出对象
exports.class1 = {
method1 : function(arg1, arg2){
console.log(arg1,arg2)
},
method2 : function (arg2, arg3, arg4){
console.log(arg2,arg3,arg4);
}
}
Run Code Online (Sandbox Code Playgroud)
你可以称之为
module1.class1.method1(arg1, arg2); //because here class1 is an object
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7146 次 |
| 最近记录: |