在ES2015中枚举通配符导入

Ani*_*ift 6 javascript ecmascript-6

因此,在ES2015中,您可以:

// Module A
export const FOO = 0;
export const BAR = 1;

// Module B
import * as AExports from 'ModuleA';
console.log(AExports.FOO); // Prints 0
Run Code Online (Sandbox Code Playgroud)

在运行时枚举ModuleA的官方方法是什么?

import * as AExports from 'ModuleA';
// Are these values guaranteed to be something?
Object.keys(AExports); // If so, should I look at enumerable values?
[...AExports]; // Iterable values?
Object.getOwnPropertyNames(AExports); // Here?
Run Code Online (Sandbox Code Playgroud)

据我所知,规范将其描述为ImportedBinding但我无法从中推断出更多内容.

NameSpaceImport : * as ImportedBinding
Let localName be the StringValue of ImportedBinding.
Let entry be the Record {[[ModuleRequest]]: module, [[ImportName]]: "*", [[LocalName]]: localName }.
Return a new List containing entry.
Run Code Online (Sandbox Code Playgroud)

log*_*yth 8

在这种情况下,规范的重要部分是当你这样做时

import * as foo from 'foo';
Run Code Online (Sandbox Code Playgroud)

foo变量的值是在创建部分15.2.1.16.4步12.B它创建了一个Module Namespace Exotic Object,其中的属性是命名出口所有的属性是可枚举的,所以你在使用完全安全Object.keys(foo)得到所有的命名出口的名称.该对象不可迭代,因此您将无法使用可迭代扩展,但您可以使用建议的对象扩展语法来复制属性(如果需要).Object.getOwnPropertyNames也应该工作得很好.