本文介绍了用JavaScript导出动态名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们必须导出大量名称.
Suppose we have to export a large amount of names.
export const fn0 = fn.bind(null, '0');
export const fn1 = fn.bind(null, '1');
...
export const fn999 = fn.bind(null, '999');
是否可以导出如下的动态名称?
Is it possible to export dynamic names like the following?
// array is [0 to 999]
array.forEach(i => export const [`fn${i}`] = fn.bind(null, i.toString());
推荐答案
let fnCollection = {};
function fn(){
return "welcome js"
}
[1,2,3,4].forEach(i =>fnCollection[`fn${i}`] = fn.bind(null, i.toString()));
export default fnCollection ;
这篇关于用JavaScript导出动态名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!