简体   繁体   English

JS ES6:声明const并将其导出为默认值与直接声明为默认导出之间的区别

[英]JS ES6: Difference between declaring a const and export it as default vs declaring it directly as default export

Is there any difference in terms of performance between declaring a constant and export it as default or declaring it directly as a default export? 在声明常量并将其导出为默认值或直接将其声明为默认导出之间,在性能方面有什么区别吗? The second one results in a cleaner code, but I'm not sure if it is treated exactly as the first case or it is created each time it is requested from another file when it's imported. 第二个代码的代码更整洁,但是我不确定它是否完全与第一种情况相同,还是在每次导入时从另一个文件中请求它时都创建了它。

For example, this code: 例如,此代码:

const myValue = { … }
export default myValue

Versus this one: 与这个:

export default { … }

Or the same using a function: 或使用功能相同:

const myFunction = (a) => { … }
export default myFunction

And: 和:

export default (a) => { … }

First off, remember that const applies to a specific variable. 首先,请记住const适用于特定变量。 It prevents assignment of a different value to that variable. 它防止为该变量分配不同的值。 The value itself is not const . 该值本身不是const The const aspect only applies to the variable itself that is declared const and what value it holds. const方面仅适用于声明为const的变量本身及其所具有的值。 So, in your example, it only applies to the actual myValue variable inside that module, not to whatever value is in the variable. 因此,在您的示例中,它仅适用于该模块内的实际myValue变量,而不适用于该变量中的任何值。

So, with this: 因此,与此:

const myValue = { … }
export default myValue

It is the myValue variable (not its value) that is const and the const aspect means that you cannot assign something different to the myValue variable. 它是myValue变量(而不是它的值)是constconst方面意味着你不能分配的东西不同myValue变量。 If you copy that same value to a different non-const variable, then one can freely assign anything you want to that other variable. 如果将同一值复制到另一个非常量变量,则可以将所需的任何内容自由分配给该另一变量。

When you export the value of that variable, it is being assigned to another variable (in whatever is importing it) and that is not const unless it is also declared const . 当导出变量的值,它被分配到另一个变量(不论是进口的话),因此不const ,除非它也宣布const The const in this module does not have any affect on some other variable in some other module that imports it. 此模块中的const对导入它的某些其他模块中的某些其他变量没有任何影响。

You can logically think of exporting and importing kind of like this as an assignment of the value to another variable (in the importing module): 您可以在逻辑上将这样的导出和导入视为将值分配给另一个变量(在导入模块中):

// exported value
const myValue = { … };        // exporting makes it available so others can import it

// imported value
let importedValue = myValue;  // importing assigns the exported value to a new variable

// further assignment
importedValue = "foo";        // this is allowed because importedValue is not 
                              // declared as const

And, as I presume you already realize, the const-ness of myValue does not make importedValue const at all. 而且,正如我想您已经意识到的那样, myValue的常数性根本不会使importedValue常数。 It contains a copy of whatever is in myValue and importedValue can be assigned any other value you want. 它包含的无论是在副本myValueimportedValue可以指定你想要的任何其他值。 It is not declared const itself so it is not const . 它没有声明为const本身,因此不是const

Is there any difference in terms of performance between declaring a constant and export it as default or declaring it directly as a default export? 在声明常量并将其导出为默认值或直接将其声明为默认导出之间,在性能方面有什么区别吗?

There is no difference for the exported value because values themselves are not const in Javascript, only variables. 导出的值没有区别,因为值本身在Javascript中不是const ,仅是变量。 The difference is only on the local variable that is declared as const which is not something the importing module can access to it makes no difference to the importing module. 区别仅在于声明为const的局部变量上,导入模块无法访问它,这与导入模块没有任何区别。

Or the same using a function 还是一样使用功能

It doesn't matter what the value of the variable is (function, object, primitive, etc...). 变量的值是什么(函数,对象,基元等)并不重要。 It's the same with all types. 所有类型都一样。 If a variable is declared const , then you cannot assign a different value to that variable. 如果将变量声明为const ,则不能为该变量分配其他值。 But if you copy that value to another variable that is not declared const , then you can further assign anything else you want to that non-const variable. 但是,如果将该值复制到未声明为const另一个变量,则可以进一步将其他任何想要的值分配给该非const变量。 It's the variable that is const , not the value. 变量是const ,而不是值。 You can think of const like declaring a read-only variable. 您可以将const声明一个只读变量。

It makes a BIG difference if your implementation fully complies to the ES6 module specification. 如果您的实现完全符合ES6模块规范,则将带来很大的不同。 Modules export bindings, not references. 模块导出绑定,而不是引用。 This is explained here: 此处说明:

https://ponyfoo.com/articles/es6-modules-in-depth#bindings-not-values https://ponyfoo.com/articles/es6-modules-in-depth#bindings-not-values

In (almost) all other aspects, Javascript purely uses references. 在几乎所有其他方面,JavaScript纯粹使用引用。 A variable is a pointer to the actual data in memory. 变量是指向内存中实际数据的指针。 Copying one variable to another copies the pointer, not the value. 将一个变量复制到另一个变量将复制指针,而不是值。 Assigning a new value to a variable creates a new data chunk and moves the variable's pointer to the new data, and the old data is garbage collected. 为变量分配新值将创建一个新的数据块,并将变量的指针移至新数据,并且旧数据将被垃圾回收。 There is a common misperception that primitives are passed by value to functions and objects by reference; 常见的误解是,基元通过值传递给函数和对象,并通过引用传递给它们。 they are in fact all passed by reference, and primitives appear to be passed by value because they are immutable -- changing a primitive discards the old value in favor of the new value, rather than changing the original value in-place. 实际上,它们都是通过引用传递的,而基元似乎是通过值传递的,因为它们是不可变的-更改基元会舍弃旧值,而使用新值,而不是就地更改原始值。

Bindings, however, are the same variable. 但是,绑定是相同的变量。 If you export something, you export IT, not a reference to it. 如果导出内容,则导出的是IT,而不是对其的引用。 If the exported value is changed later in the original module, then this change is reflected in modules that consume it. 如果稍后在原始模块中更改了导出的值,则此更改将反映在使用它的模块中。 Even worse, if another module changes the binding, it's reflected back into the original and all other consuming modules. 更糟糕的是,如果另一个模块更改了绑定,则它将反映回原始模块和所有其他使用模块。

If you're using a third-party module importer or a rollup tool, then you might not get this behavior, because it's very hard to replicate outside of the engine itself. 如果您使用的是第三方模块导入器或汇总工具,则可能不会出现此现象,因为很难在引擎本身之外进行复制。 So you might not see repercussions from this for months or years to come, but it will be a problem in the future. 因此,在接下来的几个月或几年中,您可能不会看到由此产生的影响,但是将来会是一个问题。

So it's best practice to ALWAYS EXPORT CONSTANTS to prevent any nasty surprises. 因此,最好始终导出常量以防止任何令人讨厌的意外。

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

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