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

Using script as an ES6 module results in no-undef warnings

我制作了一个脚本来生成一些虚构的账户和交易,运行脚本本身就很好。它按我的预期生成了 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

我哪里做错了,我怎样才能改进导出的工作方式?我想了解我的错误和错误,以便我可以学习更多并改进。

然后声明它们。而不是

randomint = (start, end) => {

const randomint = (start, end) => {

等等

该行为来自 javascript 严格模式。您的代码适用于 "sloppy mode"。特别是,您遇到了这条规则(取自严格模式的 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

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