admin管理员组文章数量:1335386
I would like to create a module, h
, which exports one function for every HTML element. Here's how it might be used:
import {div, p} from 'h'
const myDiv = div(p('some text'))
Here's how that module is defined:
const h = {}
for (let tagName of ['div', 'p', /* ... */]) {
h[tagName] = (...children) => {
// ...
}
}
export const div = h.div
export const p = h.p
/* ... */
I don't like that every export has to be listed explictly. How do I make these dynamic?
I would like to create a module, h
, which exports one function for every HTML element. Here's how it might be used:
import {div, p} from 'h'
const myDiv = div(p('some text'))
Here's how that module is defined:
const h = {}
for (let tagName of ['div', 'p', /* ... */]) {
h[tagName] = (...children) => {
// ...
}
}
export const div = h.div
export const p = h.p
/* ... */
I don't like that every export has to be listed explictly. How do I make these dynamic?
Share Improve this question edited May 17, 2022 at 12:03 Ashton Six asked Nov 13, 2015 at 0:49 Ashton SixAshton Six 5135 silver badges21 bronze badges 01 Answer
Reset to default 10how to name exports dynamically
You can't. import
and export
statements are specifically designed this way because they have to be statically analyzable, i.e. the import and export names have to be known before the code is executed.
If you need something dynamic then do what you are already doing: Export a "map" (or object). People can still use destructuring to just get what they want:
const {div} = h;
本文标签: javascriptes2015 moduleshow to name exports dynamicallyStack Overflow
版权声明:本文标题:javascript - es2015 modules - how to name exports dynamically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742386312a2465104.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论