【发布时间】:2019-04-02 09:13:39
【问题描述】:
我制作了一个脚本来生成一些虚构的帐户和交易,并且自己运行该脚本就可以了。它按我的预期生成 2 个列表,但是我需要在另一个文件中使用这些列表。当我在最后导出变量并将它们重新导入另一个文件时,我收到一堆 no-undef 警告并且我的构建失败。
我假设这是因为我的导出对象包含函数。如何强制函数只生成值以便正确导出它们?
randomint = (start, end) => {
let diff = end - start;
return Math.floor(Math.random() * diff) + start
}
chance = (rate=0.5) => {
return Math.random() > rate ? true : false;
}
pad = (n, width, z) => {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
NUM_OF_ACCOUNTS = 10
NUM_OF_TXS = randomint(30, 40)
let accounts = [];
let transactions = [];
for (let i=0; i< NUM_OF_ACCOUNTS; i++) {
accounts.push({
id: i,
ref: `SMAR_A${pad(i, 3)}`,
account: randomint(10000000, 99999999),
sortcode: randomint(100000, 9999999),
fraud: chance(0.1),
balance: Math.round(Math.random() * 85000, 2)
})
}
for (let t = 0; t < NUM_OF_TXS; t++) {
// Lookup a random account number to generate a transaction for
acct_num = randomint(0, accounts.length - 1 )
transactions.push({
ref: accounts[acct_num].ref,
deposit: Math.round(Math.random() * 85000, 2),
account: accounts[acct_num].account,
sortcode: accounts[acct_num].sortcode,
})
};
export accounts;
export transactions;
我尝试了一系列的导出和导入,但都没有运气。
Line 1: 'randomint' is not defined no-undef
Line 6: 'chance' is not defined no-undef
Line 10: 'pad' is not defined no-undef
Line 16: 'NUM_OF_ACCOUNTS' is not defined no-undef
Line 17: 'NUM_OF_TXS' is not defined no-undef
Line 17: 'randomint' is not defined no-undef
Line 23: 'NUM_OF_ACCOUNTS' is not defined no-undef
Line 26: 'pad' is not defined no-undef
Line 27: 'randomint' is not defined no-undef
Line 28: 'randomint' is not defined no-undef
Line 29: 'chance' is not defined no-undef
Line 34: 'NUM_OF_TXS' is not defined no-undef
Line 35: 'acct_num' is not defined no-undef
Line 35: 'randomint' is not defined no-undef
Line 38: 'acct_num' is not defined no-undef
Line 40: 'acct_num' is not defined no-undef
Line 41: 'acct_num' is not defined no-undef
我做错了什么?如何改进导出的工作方式?我想了解我的错误和错误,以便了解更多信息并进行改进。
【问题讨论】:
标签: javascript ecmascript-6 es6-modules