gpt4 book ai didi

javascript - 使用脚本作为 ES6 模块会导致 no-undef 警告

转载 作者:行者123 更新时间:2023-11-29 17:37:14 25 4
gpt4 key购买 nike

我制作了一个脚本来生成一些虚构的账户和交易,并且可以自行运行该脚本。它按我的预期生成了 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 严格模式。您的代码在“草率模式”下工作。特别是,您遇到了这条规则(取自严格模式的 Mozilla documentation):

Strict mode makes it impossible to accidentally create global variables. In normal JavaScript mistyping a variable in an assignment creates a new property on the global object and continues to "work" (although future failure is possible: likely, in modern JavaScript). Assignments, which would accidentally create global variables, instead throw an error in strict mode:

在您的代码中,这发生在这里:

randomint = (start, end) => {
let diff = end - start;
return Math.floor(Math.random() * diff) + start
}

以及所有其他不使用 constletvar 引入变量的地方。

这是一个简单的修复,只需在每个变量前添加 constlet 即可:

const randomint = (start, end) => {
let diff = end - start;
return Math.floor(Math.random() * diff) + start
}

const chance = (rate=0.5) => {
return Math.random() > rate ? true : false;
}
// etc

你只会在模块中遇到这种情况,因为模块默认启用严格模式,而普通脚本不会。

关于javascript - 使用脚本作为 ES6 模块会导致 no-undef 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55471007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com