【问题标题】:Class array for loop fails循环的类数组失败
【发布时间】:2020-07-15 16:22:43
【问题描述】:

当我尝试使用 for 循环时,这段代码失败了

class TestA {
  testArray = [];
  constructor() {
    this.testArray.push({
      name: "joe",
      age: 70
    });
    this.testArray.push({
      name: "mike",
      age: 50
    });
    this.testArray.push({
      name: "bob",
      age: 33
    });
  }
  testLoop() {
    for (test of this.testArray) {
      console.log(" >>> " + test.name + " " + test.age);
    }
  }
}
var a = new TestA();
a.testLoop();

错误:

Uncaught ReferenceError: test is not defined

我可以通过使用不同类型的循环来解决这个问题,但不明白为什么这不起作用?

【问题讨论】:

  • for (const test of this.testArray)
  • 也许把test of改成let test of
  • 应该是:for (let test of ....)
  • 我要指出,您不需要多次连续调用.push()。只需将所有对象作为参数添加到单个调用中,它们都会被添加到数组中。 this.testArray.push({...}, {...}, {...})
  • @Coder547:我认为在class 定义中,您是在严格模式 下运行的,它设置了更严格的要求。在class 之外,您可以使用"use strict" 声明性手动将代码置于该模式。

标签: javascript


【解决方案1】:

您忘记使用letconst 声明test

如果您希望循环主体中的标识符是只读的,请使用 const(例如,如果有人稍后修改代码以添加赋值,则在严格模式下这是一个主动错误)。如果您希望能够分配给它,请使用 let(因为您的代码中有一个分配,或者您希望有人能够稍后添加一个而不更改声明)。

摘自:for...of loop. Should I use const or let?

class TestA {
  testArray = [];
  constructor() {
    this.testArray.push({
      name: "joe",
      age: 70
    });
    this.testArray.push({
      name: "mike",
      age: 50
    });
    this.testArray.push({
      name: "bob",
      age: 33
    });
  }
  testLoop() {
    for (let test of this.testArray) {
      console.log(" >>> " + test.name + " " + test.age);
    }
  }
}
var a = new TestA();
a.testLoop();

【讨论】:

  • 在这种情况下,他也可以使用const关键字
【解决方案2】:

您应该为该行写 let

for (let test of this.testArray) {

【讨论】:

    【解决方案3】:

    for..of statement 中,您需要使用constletvar 声明循环变量(test):

    class TestA {
      testArray = [];
      constructor() {
        this.testArray.push({
          name: "joe",
          age: 70
        });
        this.testArray.push({
          name: "mike",
          age: 50
        });
        this.testArray.push({
          name: "bob",
          age: 33
        });
      }
      testLoop() {
        for (let test of this.testArray) {
          console.log(" >>> " + test.name + " " + test.age);
        }
      }
    }
    var a = new TestA();
    a.testLoop();

    【讨论】:

      【解决方案4】:

      在构造函数中绑定函数,因为this没有被保留。

      class TestA {
        testArray = [];
        constructor() {
          this.testArray.push({
            name: "joe",
            age: 70
          });
          this.testArray.push({
            name: "mike",
            age: 50
          });
          this.testArray.push({
            name: "bob",
            age: 33
          });
          
          this.testLoop = this.testLoop.bind(this);
        }
        testLoop() {
          for (const test of this.testArray) {
            console.log(" >>> " + test.name + " " + test.age);
          }
        }
      }
      var a = new TestA();
      a.testLoop();

      【讨论】:

      • 在 OP 的情况下,这完全无关,也没有必要。
      • @YuryTarabanko 但他试图引用this.testArray 对,这将是未定义的
      • 不会的。他正在使用这种方法,例如a.testLoop(),因此它将a 作为上下文。你可以试试。唯一的问题是 OP 没有声明循环变量并且类主体是在严格模式下评估的。
      • 如果a.testLoop() 没有正确地将this 设置为a 对象,这将使方法几乎无用。如果要在调用它之前将方法与对象分开,则需要绑定它。
      猜你喜欢
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 2014-04-23
      • 1970-01-01
      相关资源
      最近更新 更多