我们如何从JavaScript模块导出常数列表和函数
我必须导出2种类型的常数和诸如Getuser之类的功能。
myModule.js
const type1 = require('./constants1.js');
const type2 = require('./constants2.js');
module.exports = Object.freeze(Object.assign(Object.create(null), type1, type2))
常数
module.exports = Object.freeze({
DOUBLE: 1,
FLOAT: 2
})
1.js computer.js
const udb = require('./mymodule.js');
console.log(udb.DOUBLE);
现在我也想导出诸如getuser,我如何更改mymodule.js的功能也可以导出函数,以便消费者可以调用udb.getuser
。请建议。
module.exports = Object.freeze(Object.assign(Object.create(null), type1, type2)), getUser: function() {}
I have to export 2 type of constants and few functions like getUser.
mymodule.js
const type1 = require('./constants1.js');
const type2 = require('./constants2.js');
module.exports = Object.freeze(Object.assign(Object.create(null), type1, type2))
constants1.js
module.exports = Object.freeze({
DOUBLE: 1,
FLOAT: 2
})
consumer.js
const udb = require('./mymodule.js');
console.log(udb.DOUBLE);
Now I also want to export function like getUser , how do i change mymodule.js to export the functions too , so that consumer.js can call udb.getUser
something like this but it doesnt work, Please suggest.
module.exports = Object.freeze(Object.assign(Object.create(null), type1, type2)), getUser: function() {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
常数1.js
constant2.js
mymodules.js
consumer.js
edit:添加完整的示例
编辑2:更简单
constant1.js
constant2.js
mymodules.js
consumer.js
EDIT: Added a complete example
EDIT 2: Simpler
您可以使用传播操作员
You could use spread operator