为什么JavaScript具有默认导出功能? [英] Why does JavaScript have default exports?

查看:116
本文介绍了为什么JavaScript具有默认导出功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript有两种导出类型:普通 [1] 和默认类型。

JavaScript has two types of exports: normal[1] and default.

编辑:JavaScript具有两种类型的导出语法

EDIT: JavaScript has two types of export syntaxes

class Foo {...}
class Bar {...}

export {
  Foo,
  Bar,
};

export class Foo {...}
export class Bar {...}

可以通过两种方式导入常规导出:名称空间 [1] 导入和命名导入(外观类似于解构)。

Normal exports can be imported in two ways: namespace[1] imports and named imports (looks similar to destructuring).

命名空间导入

import * as Baz from './baz';

命名进口

import {Foo, Bar} from './baz';



默认出口



Default exports

class Foo {...}
class Bar {...}

export default {
  Foo,
  Bar,
};

默认导出还可以通过两种方式导入:名称空间导入和命名导入(使用

Default exports can also be imported in two ways: namespace imports and named imports (in a hackish way using destructuring in a separate statement)

命名空间导入

import Baz from './baz';

命名进口

import temp_Baz from './baz';
const {Foo, Bar} = temp_Baz;






问题



正常导出和默认导出都具有相同的功能-它们可以导入名称空间,也可以分解为较小的碎片。


Question

Both normal exports and default exports have the same functionality - they can be imported into a namespace, and they can be destructured into smaller pieces.

1 )为什么JavaScript具有默认导出功能而不是仅遵循常规导出功能并具有以下导入语法的原因是什么?

1) What are the reasons JavaScript has default exports, instead of sticking with just normal exports and having the import syntax be the following?

import <varName> from <location>;

< varName> 会允许像

Node.js在没有默认导出的情况下进行管理,唯一的附加更改是允许导出单个值:

Node.js managed without default exports, and the only additional change would be to allow exporting of a single value:

module.exports = class Foo {...}

这可以通过 export = export 完成或类似的内容:

This could be done with export = or export only or something similar:

export only class Foo {...}

所以现在您可能会问,仅导出 export有什么区别默认值

So now you might ask, what is the difference between export only and export default?


  • 仅导出可以导入导出具有相同的语法,而导出默认值需要不同的导入语法。为了使两种语法都对模块用户有效,模块必须同时 export export default 相同的项目(其中是我在所有模块中所做的事情。)

  • 导出默认值重命名为仅导出在我看来这是一个更好的术语(不太混乱;更清晰)

  • export only exports could be imported with the same syntax, whereas export default requires a different importing syntax. To allow both syntaxes to work for the module user, the module must both export and export default the same items (which is something I do in all my modules).
  • export default is renamed to export only which is a better term in my opinion (less confusing; clearer)

2)上面列出的所有区别都是优点。缺点还有其他区别吗?还是上述任何区别也是不利的?

2) All the differences listed above are pros. Are there any other differences that are cons? Or can any of the above differences also be cons?

似乎我误解了预期用于导出默认值的用途。

It seems I misunderstood the intended use for export default.

所以我现在想知道什么时候使用默认出口?虽然我可能不应该在此问题上添加更多内容。

So I'd now like to know when are default exports used? Although I probably shouldn't add more to this question.

如果默认导出的唯一用途是模块仅导出单个项目,则此问题仍然存在

If the only use for default exports is if your module only has a single item to export, then this question still applies.

现在,如果的预期用途是,一个模块仅导出单个项目。所以我的问题仍然适用。为什么不将默认导出替换为仅导出,而无需其他默认导入语法?

Now it seems that the intended use is if a module only exports a single item. So my question still applies. Why not replace default exports with export only, removing the need for an additional default import syntax?

[1] 我不确定这是否是正确的用语

[1] I am not sure if this is the correct term to use

推荐答案


JavaScript有两种导出类型:普通和默认。

JavaScript has two types of exports: normal and default.

不,不是。 JavaScript只有一种导出结构。

No, not really. JavaScript has only one kind of export structure. It has different syntaxes though.

导出默认函数x(){} 只是的简写。函数x(){};导出{x为默认值}

从'...'导入x只是<$ c的简写$ c> import {默认为x}从'...'

您讨论过的两个模块导出之间的差异远大于单个默认关键字。它们看起来如此相似,因为您使用了速记符号。完全清楚地说明了,这是

The difference between your the two module exports you discussed is much bigger than the single default keyword. They only look so similar because you used shorthand notations. Fully spelled out, it's

export {
  Foo as Foo,
  Bar as Bar,
} // a declaration

vs

export default ({
  Foo: Foo,
  Bar: Bar,
}); // an object literal



正常导出和默认导出都具有相同的功能-它们可以可以导入到名称空间中,然后可以将它们分解成较小的块。

Both normal exports and default exports have the same functionality - they can be imported into a namespace, and they can be destructured into smaller pieces.

不,它们不是。导入为导出的变量声明提供别名,没有解构-并且您不能对默认导出对象的属性执行此操作。

No, they don't. Imports provide aliases for the exported variable declarations, there's no destructuring - and you cannot do that to properties of a default-exported object.


JavaScript具有默认导出而不是仅坚持常规导出的原因是什么?并具有以下导入语法?

What are the reasons JavaScript has default exports, instead of sticking with just normal exports and having the import syntax be the following?

请参见 https://esdiscuss.org/topic/moduleimport 。 default-export语法为使用特殊名称 default (否则不是有效的变量名)进行导出提供了一种简写语法,因为这是导出的一种很常见的用例一个模块中只有一件事。无论如何,当此模块中只有一个值时,不需要显式命名值。在这方面,它非常有您为仅导出建议所设想的目的,但它只是一个导出,没有提供多个可别名导出的完整命名空间。

See https://esdiscuss.org/topic/moduleimport. The default-export syntax provides a shorthand syntax for exporting under the special name default (which is not a valid variable name otherwise), as it is a quite common use case to export only a single thing from a module. It doesn't require explicitly naming a value when there is only one in this module anyway. In this regard, it has very much the purpose you envisioned for your export only suggestion, but it is only one export not providing a full namespace of multiple aliasable exports.

这篇关于为什么JavaScript具有默认导出功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆