简体   繁体   English

节点js文件中有多个module.exports

[英]Multiple module.exports in a node js file

How does the Nodejs engine decides when a file has multiple module.exports and it is being required by another file ? Nodejs引擎如何确定文件何时具有多个module.exports以及另一个文件是否需要它? How does the final closure is being calculated ? 如何计算最终关闭?

It uses the value that module.exports has when the execution of the module has finished: 当模块执行完成时,它将使用module.exports具有的值:

module.exports = { foo: 'bar' };
module.exports = { a: 17.8 };    // overwrite "module.exports"
module.exports = 5;              // and again 
module.exports = { b: 123 };     // and again

Now, when you require the module, you will get { b: 123 } . 现在,当您需要该模块时,将得到{ b: 123 }

module.exports is not a magic keyword, it's just a variable that you can set to tell NodeJS what should be exported. module.exports不是魔术关键字,它只是一个变量,您可以将其设置为告诉NodeJS应该导出什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM