我们如何从JavaScript模块导出常数列表和函数

发布于 2025-02-13 00:54:22 字数 669 浏览 2 评论 0原文

我必须导出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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不在是我 2025-02-20 00:54:22

常数1.js

module.exports = Object.freeze({
    DOUBLE: 1,
    FLOAT: 2
});

constant2.js

module.exports = Object.freeze({
    TRIPLE: 3,
});

mymodules.js

const getUser = ()=> console.log("Something");
const getId = ()=>console.log("Something2");

const type1 = require("./constants1.cjs");
const type2 = require("./constants2.cjs");

const udb = Object.assign(Object.create(null), type1, type2);
udb.getUser = getUser;
udb.getId = getId;
module.exports = Object.freeze( udb );

consumer.js

const udb = require("./mymodule.cjs");
console.log(udb.DOUBLE);

// udb.getUser();                    
// udb.getId();                    

edit:添加完整的示例

编辑2:更简单

constant1.js

module.exports = Object.freeze({
    DOUBLE: 1,
    FLOAT: 2
});

constant2.js

module.exports = Object.freeze({
    TRIPLE: 3,
});

mymodules.js

const getUser = ()=> console.log("Something");
const getId = ()=>console.log("Something2");

const type1 = require("./constants1.cjs");
const type2 = require("./constants2.cjs");

const udb = Object.assign(Object.create(null), type1, type2);
udb.getUser = getUser;
udb.getId = getId;
module.exports = Object.freeze( udb );

consumer.js

const udb = require("./mymodule.cjs");
console.log(udb.DOUBLE);

// udb.getUser();                    
// udb.getId();                    

EDIT: Added a complete example

EDIT 2: Simpler

雪若未夕 2025-02-20 00:54:22

您可以使用传播操作员

const type1 = require('./constants1.js');
const type2 = require('./constants2.js');

function getId() {}
function getUser() {}

module.exports = Object.freeze({ getId, getUser, ...type1, ...type2 })

You could use spread operator

const type1 = require('./constants1.js');
const type2 = require('./constants2.js');

function getId() {}
function getUser() {}

module.exports = Object.freeze({ getId, getUser, ...type1, ...type2 })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文