简体   繁体   English

节点中module.exports之间的区别

[英]Difference between module.exports in node

What is the difference between 之间有什么区别

module.exports = UpgradeService;

and

module.exports = { UpgradeService };

When I used the second one, I wasn't able to peek its definition in VS code. 当我使用第二个时,我无法在VS代码中窥视它的定义。 Why this is happening and what are the similarities and differences between them? 为什么会这样,它们之间有什么异同?

The first statement sets the exported value to UpgradeService . 第一条语句将导出的值设置为UpgradeService The second statement sets the exported value to an object. 第二条语句将导出的值设置为对象。 The { UpgradeService } is a shorthand for { UpgradeService: UpgradeService } which is a simple key:value pair! { UpgradeService }{ UpgradeService: UpgradeService }的简写,它是一个简单的key:value对! In other words, it exports a plain object that has only one (own) key: UpgradeService . 换句话说,它导出仅具有一个(自己)键的普通对象: UpgradeService

Remember that setting module.exports = (something) really is just changing what you get when you require() the module, and that (something) can be any value. 请记住,设置module.exports = (something)实际上只是在更改您require()模块时得到的(something) ,并且(something)可以是任何值。 You could set module.exports = 42 and require() would return the number 42 just fine. 您可以设置module.exports = 42require()将返回数字42就可以了。

Doing module.exports = { UpgradeService } means that you're setting the export to an object , which looks like {"UpgradeService": UpgradeService} . 执行module.exports = { UpgradeService }意味着您正在将导出设置为一个对象 ,该对象类似于{"UpgradeService": UpgradeService} That follows the ES6 syntax rule where {x, y} is the same as {x: x, y: y} . 这遵循ES6语法规则 ,其中{x, y}{x: x, y: y}

Then in your other files, instead of doing const UpgradeService = require('blah') , you do const UpradeService = require('blah').UpradeService , or const { UpgradeService } = require('blah') with destructuring . 然后在其他文件中,执行const UpradeService = require('blah').UpradeServiceconst { UpgradeService } = require('blah')进行销毁 ,而不是执行const UpgradeService = require('blah')

Usually you set module.exports to an object (instead of a function or class) when you plan on exporting multiple things. 通常,在计划导出多个对象时,将module.exports设置为一个对象(而不是函数或类)。 For example, you might want to export both UpgradeService and, later, a new Upgrade class; 例如,您可能要导出UpgradeService和以后的新Upgrade类。 in that case, you would do module.exports = { UpgradeService, Upgrade } . 在这种情况下,您将执行module.exports = { UpgradeService, Upgrade }

Some people prefer to always start with exporting an object like that, because then it's easy to add a new exported thing. 有些人倾向于始终从导出这样的对象开始,因为这样很容易添加新的导出对象。 For instance, if you change module.exports = 'Apple' to module.exports = { fruit: 'Apple', animal: 'Bat' } , you have to change all files which required that module. 例如,如果将module.exports = 'Apple'更改为module.exports = { fruit: 'Apple', animal: 'Bat' } ,则必须更改所有需要该模块的文件。 But if you had just started with module.exports = { fruit: 'Apple' } , you would be able to add animal: 'Bat' without having to change any existing files. 但是,如果您刚开始使用module.exports = { fruit: 'Apple' } ,则可以添加animal: 'Bat'而无需更改任何现有文件。

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

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