Javascript 使用导出对象导出对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12469723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exporting Objects with the Exports Object
提问by Connor Black
Say I have one .jsfile containing a javascript object. I want to be able to access that object and all of its functionality from another .jsfile in the same directory. Can I simply export this object with the module.exportsobject and require()it in the other .jsfile? If this is possible can you give me an example?
假设我有一个.js包含 javascript 对象的文件。我希望能够从.js同一目录中的另一个文件访问该对象及其所有功能。我可以简单地用导出这个目标module.exports对象,并require()在另一方面,它.js的文件?如果这是可能的,你能给我一个例子吗?
If it helps I'm developing with node.
如果它有帮助,我正在使用 node.js 进行开发。
回答by c0deNinja
This is the way I create modules:
这是我创建模块的方式:
myModule.js
我的模块.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;
And to use this module in another JS file, you can simply use requireand use your object as normal:
并且要在另一个 JS 文件中使用此模块,您可以require像往常一样简单地使用和使用您的对象:
someFile.js
someFile.js
var myObject = require('myModule');
myObject.function1(param1, function(err, result) {
...
});
回答by Vahid PG
Of course you can. In my example I use obj to hold my config info. I put it in a file called index.jsin configfolder. This makes the index the preferred choice to be picked when I import 'config'. I have 2 exports here one for my node and api stuff and the other for my db. You can ignore the first bit where I set the environment.
当然可以。在我的示例中,我使用 obj 来保存我的配置信息。我把它放在一个名为文件index.js在config文件夹中。这使得索引成为 I 时选择的首选import 'config'。我在这里有 2 个导出,一个用于我的节点和 api 内容,另一个用于我的数据库。您可以忽略我设置环境的第一位。
const environment = {
development: {
isProduction: false
},
production: {
isProduction: true
}
}[ process.env.NODE_ENV || 'development' ];
export default Object.assign({
host: 'localhost',
port: '3000',
remoteApi: {
token: {
'X-Token': '222222222222222222'
},
base: 'https://www.somedomain.com/api'
}
}, environment);
export const db = {
dbHost: 'localhost',
dbPort: 176178
};
Calling import config from '../config';will pick the default one. And if I specify I can get the dbexport import { db } from '../config';
调用import config from '../config';将选择默认的。如果我指定我可以获得db导出import { db } from '../config';
回答by JoshRagem
In one file:
在一个文件中:
module.exports.myObj = some object...;
In the other:
在另一个:
Obj = require('myFile.js').myObj;
Everything in a js file on node is local to that file unless you put it in the export object. This actually is very different from JavaScript in a browser--in the browser all files that get imported act together like one big file.
除非你把它放在导出对象中,否则节点上的 js 文件中的所有内容都是该文件的本地文件。这实际上与浏览器中的 JavaScript 非常不同——在浏览器中,所有导入的文件都像一个大文件一样。
You can kinda think about node files as though you are creating a module object and passing it' into a function surrounding your code.
您可以将节点文件视为创建模块对象并将其传递给围绕您的代码的函数。
module = { 'exports' : {} };
(function(module){
//your js file
...
})(module)
回答by OhadR
the simplest approach, in my opinion:
在我看来,最简单的方法是:
write Person.js: (note it comes with ctor)
编写 Person.js :(注意它带有 ctor)
module.exports = function (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = function () {
return this.firstName + ' ' + this.lastName;
}
}
and consume it:
并消费它:
var person = require('./Person.js');
var person1 = new person('James', 'Bond');
console.log(person1.fullName());
note that in this simple solution all methods are "public".
请注意,在这个简单的解决方案中,所有方法都是“公共的”。
ref: https://www.tutorialsteacher.com/nodejs/nodejs-module-exports
参考:https: //www.tutorialsteacher.com/nodejs/nodejs-module-exports

