简体   繁体   English

在 javascript 中导出 function 或 object 有什么区别?

[英]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.

相关问题 javascript函数和javascript对象之间的主要核心区别是什么? - What is the main core difference between a javascript function and javascript object? 在javascript对象中创建函数的不同方法有什么区别? - What is the difference between different ways to create a function inside javascript object? Javascript:使用的区别是什么? 和:用于在函数或对象中指定变量的运算符? - Javascript: What is the difference between usage of . and : operators for specifying a variable in a function or object? 这两种在JavaScript函数中利用arguments对象的方式有什么区别? - What is the difference between these two ways to leverage the arguments object in a JavaScript function? javascript中函数和对象的本质区别是什么? - What's substantive difference between function and Object in javascript? JavaScript对象和函数之间的区别 - Difference between javascript object and function 这两种从Javascript模块导出的方式之间有什么区别? - What is the difference between these 2 ways of exporting from a Javascript module? 函数对象和可调用对象之间有什么区别? - What is the difference between a function object and a callable object? JavaScript Function()和JavaScript对象之间的区别 - Difference between a JavaScript Function() and a JavaScript object 函数和对象函数有什么区别 - What is the difference between function and object function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM