admin管理员组文章数量:1406727
I'm wanting to programmatically generate the exports for a module, is this possible in es6?
Something along these lines:
const ids = ['foo', 'bar', 'baz'];
ids.forEach(id => {
export const [id.toUpperCase()] = id;
});
I'm wanting to programmatically generate the exports for a module, is this possible in es6?
Something along these lines:
const ids = ['foo', 'bar', 'baz'];
ids.forEach(id => {
export const [id.toUpperCase()] = id;
});
Share
Improve this question
asked Mar 8, 2016 at 18:35
TomTom
44.9k4 gold badges43 silver badges61 bronze badges
1
- possibly related: What qualifies as being a dynamic export in ES6 – Bergi Commented Mar 8, 2016 at 18:46
2 Answers
Reset to default 8No, it's not. Exports and imports are required to be statically analysable in ES6 modules.
Not only is that no-top-level export
declaration a syntax error, but also your attempt at declaring variables with dynamic names. The bracket notation is reserved for puted properties only.
So if you are going to programmatically generate module exports, you'll need to dynamically generate the module source text (as a part of your build process).
You could export an object which has dynamic keys but then you would have to destructure it after the import.
const ids = ['foo', 'bar', 'baz'].reduce(...code to reduce to what you want);
export default ids; // { FOO: 'foo', BAR: 'bar', BAZ: 'baz' }
import ids from './ids'
const { BAR } from ids;
本文标签: javascriptGenerating es6 module exportsStack Overflow
版权声明:本文标题:javascript - Generating es6 module exports - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745035388a2638753.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论