【问题标题】:Using script as an ES6 module results in no-undef warnings将脚本用作 ES6 模块会导致无 undef 警告
【发布时间】: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


    【解决方案1】:

    该行为来自 javascript 严格模式。您的代码在“草率模式”下工作。特别是你遇到了这条规则(取自严格模式的Mozilla documentation):

    严格模式使得不可能意外创建全局 变量。在正常的 JavaScript 中,在赋值中输入错误的变量 在全局对象上创建一个新属性并继续“工作” (尽管未来的失败是可能的:在现代 JavaScript 中很可能)。 分配,它会意外地创建全局变量,而不是 在严格模式下抛出错误:

    在您的代码中,这发生在此处:

    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
    

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

    【讨论】:

    • 感谢您的精彩解释!我的代码一直很草率!我真的应该在发布之前正确阅读错误。猜猜它会帮助其他遇到同样问题的人!
    【解决方案2】:

    然后声明它们。而不是

    randomint = (start, end) => {
    

    const randomint = (start, end) => {
    

    等等

    【讨论】:

    • 已经解决了。我不敢相信我是这样的白痴!由于某种原因,我什至没有想到这个问题。谢谢,以后我会阅读错误并真正思考它的含义!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    相关资源
    最近更新 更多