[英]What is the difference between exporting a function or an object in javascript?
const oneConfig = { ...someParams }
const oneFunction = function (){ return oneConfig }
1. export oneConfig;
2. export oneFunction;
If I have an object that needs to be exported, then there seem to be two ways.如果我有一个需要导出的object,那么似乎有两种方法。 What is the difference?
有什么区别? What is the difference between memory usage, performance, and js engine processing?
memory使用、性能、js引擎处理有什么区别?
If I just import the file but don't execute it immediately如果我只是导入文件但不立即执行它
As the comments (by Jaromanda X and Gabriele Petrioli) point out, the function essentially adds a layer of indirection around the object.正如评论(由 Jaromanda X 和 Gabriele Petrioli 撰写)所指出的,function 实质上在 object 周围增加了一层间接性。 Exporting the object directly is slightly more efficient in terms of performance, because then the importer doesn't have to call the function;
直接导出 object 在性能方面效率稍高一些,因为这样导入器就不必调用 function; it is slightly more memory efficient if you don't need the function for anything else and can simply drop it.
如果您不需要 function 来做其他事情,那么 memory 的效率会更高一些,并且可以简单地放弃它。
That said, the differences are too small to matter;也就是说,差异太小了。 you should do whichever makes more sense for your application.
你应该做任何对你的应用程序更有意义的事情。 If wrapping things in a function is more convenient, or more consistent, or otherwise preferable, then do that and don't worry about performance;
如果将东西包装在 function 中更方便、更一致或更可取,那么就这样做,不要担心性能; on the other hand if you don't need the function (and it's simpler without), why add something you don't need?
另一方面,如果您不需要 function(没有它更简单),为什么要添加您不需要的东西?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.