简体   繁体   English

导入/导出名称冲突解决

[英]Import/Export name collision resolution

Testing in Node JS the following modules layout, looks like local exported definitions always replace external exported in case of name collision (see f1 in B.js).在 Node JS 中测试以下模块布局,看起来本地导出的定义总是在名称冲突的情况下替换外部导出的定义(参见 B.js 中的 f1)。

A.js js

export const f1 = 'A'

B.js B.js

export * from './A.js'
export const f1 = 'B'

C.js C.js

import * as A from './A.js'
import * as B from './B.js'
console.log(A.f1)
console.log(B.f1)
> node C.js
// A
// B

Is this a rule?这是规则吗? I have not found something about how to manage this in Ecmascript specs.我还没有在 Ecmascript 规范中找到有关如何管理此问题的信息。

Does import/export order matter?进口/出口订单重要吗?

Do you see this as a reliable method for extending a module overloading functions and/or adding new ones?您认为这是扩展模块重载功能和/或添加新功能的可靠方法吗?

Is this a rule?这是规则吗? I have not found something about how to manage this in Ecmascript specs.我还没有在 Ecmascript 规范中找到有关如何管理此问题的信息。

Yes, Local exports have priority.是的,本地出口优先。 Which is, in fact, standardized in the spec :事实上,这在规范中是标准化的:

  1. For each ExportEntry Record e in module .[[LocalExportEntries]], do对于模块.[[LocalExportEntries]] 中的每个 ExportEntry Record e ,执行
    a.一个。 Assert : module provides the direct binding for this export. 断言模块为此导出提供直接绑定。
    b.湾。 Append e .[[ExportName]] to exportedNames . Append e .[[ExportName]] 到exportsNames
  2. For each ExportEntry Record e in module .[[IndirectExportEntries]], do对于模块.[[IndirectExportEntries]] 中的每个 ExportEntry Record e ,执行
    a.一个。 Assert : module imports a specific binding for this export. 断言模块为此导出导入特定绑定。
    b.湾。 Append e .[[ExportName]] to exportedNames . Append e .[[ExportName]] 到exportsNames

Specifically the starExport in your case is part of:具体来说,您的案例中的starExport是以下内容的一部分:

For each ExportEntry Record e in module.[[StarExportEntries]], do
    (...)
    c. Let starNames be requestedModule.GetExportedNames(exportStarSet).
    d. For each element n of starNames, do
        i. If SameValue(n, "default") is false, then
            1. If n is not an element of exportedNames, then
                a. Append n to exportedNames.

So, to answer your second question:所以,回答你的第二个问题:

Do you see this as a reliable method for extending a module overloading functions and/or adding new ones?您认为这是扩展模块重载功能和/或添加新功能的可靠方法吗?

Yes, it's reliable because it's specified in the standard.是的,它是可靠的,因为它是在标准中指定的。

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

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