admin管理员组文章数量:1389972
So, in ES2015 you can have:
// Module A
export const FOO = 0;
export const BAR = 1;
// Module B
import * as AExports from 'ModuleA';
console.log(AExports.FOO); // Prints 0
What's the official way to enumerate the exports of ModuleA at runtime?
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?
As far as I can tell, the spec describes this as an ImportedBinding but I can't deduce anything more from that.
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.
So, in ES2015 you can have:
// Module A
export const FOO = 0;
export const BAR = 1;
// Module B
import * as AExports from 'ModuleA';
console.log(AExports.FOO); // Prints 0
What's the official way to enumerate the exports of ModuleA at runtime?
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?
As far as I can tell, the spec describes this as an ImportedBinding but I can't deduce anything more from that.
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.
Share
Improve this question
asked Jul 30, 2016 at 22:56
intentionally-left-nilintentionally-left-nil
8,3468 gold badges40 silver badges62 bronze badges
1 Answer
Reset to default 10The important part of the spec in this case is that when you do
import * as foo from 'foo';
The foo
variable's value is created at section 15.2.1.16.4 step 12.b which creates a Module Namespace Exotic Object
, where properties are the named exports and all properties are enumerable so you are totally safe in using Object.keys(foo)
to get the names of all of the named exports. The object is not iterable, so you will not be able to use an iterable spread, though you could use the proposed object spread syntax to copy the properties if you wanted. Object.getOwnPropertyNames
should work fine too.
本文标签: javascriptEnumerating wildcard imports in ES2015Stack Overflow
版权声明:本文标题:javascript - Enumerating wildcard imports in ES2015 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744710973a2621126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论