【发布时间】:2015-11-13 00:49:49
【问题描述】:
在此示例中,react-hyperscript 被柯里化并公开了一组默认函数,因此 h('div', props, children) 变为 div(props, children)。
import hyperscript from 'react-hyperscript';
import {curry} from 'lodash';
const isString = v => typeof v === 'string' && v.length > 0;
const isSelector = v => isString(v) && (v[0] === '.' || v[0] === '#');
const h = curry(
(tagName, first, ...rest) =>
isString(tagName) && isSelector(first) ?
hyperscript(tagName + first, ...rest) :
hyperscript(tagName, first, ...rest)
);
const TAG_NAMES = [
'a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', // ...
];
TAG_NAMES.forEach(tagName =>
Object.defineProperty(h, tagName, {
value: h(tagName),
writable: false,
})
);
export default h;
在另一个模块中:
import h, {div} from 'lib/h';
console.log(
h, // h
div, // undefined <- problem!
h('div'), // div
h.div // div
)
这可以通过将其附加到示例中来解决(来自 lodash 的 zip):
const {
a, abbr, address, area, // ...
} = zip(
TAG_NAMES,
TAG_NAMES.map(h)
)
export {
a, abbr, address, area, // ...
}
但是这个解决方案不是很优雅,有人知道更好的选择吗?
【问题讨论】:
标签: javascript module ecmascript-6