使用导出对象导出对象 [英] Exporting Objects with the Exports Object
问题描述
说我有一个.js
文件,其中包含一个javascript对象.我希望能够从同一目录中的另一个.js
文件访问该对象及其所有功能.我可以简单地使用module.exports
对象将其导出并在另一个.js
文件中将其导出吗?如果可以的话,您能举个例子吗?
Say I have one .js
file containing a javascript object. I want to be able to access that object and all of its functionality from another .js
file in the same directory. Can I simply export this object with the module.exports
object and require()
it in the other .js
file? If this is possible can you give me an example?
如果有帮助,我可以使用node进行开发.
If it helps I'm developing with node.
推荐答案
这是我创建模块的方式:
This is the way I create modules:
myModule.js
myModule.js
var MyObject = function() {
// This is private because it is not being return
var _privateFunction = function(param1, param2) {
...
return;
}
var function1 = function(param1, callback) {
...
callback(err, results);
}
var function2 = function(param1, param2, callback) {
...
callback(err, results);
}
return {
function1: function1
,function2: function2
}
}();
module.exports = MyObject;
要在另一个JS文件中使用此模块,您可以简单地使用require
并像平常一样使用您的对象:
And to use this module in another JS file, you can simply use require
and use your object as normal:
someFile.js
someFile.js
var myObject = require('myModule');
myObject.function1(param1, function(err, result) {
...
});
这篇关于使用导出对象导出对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!