【问题标题】:TypeError: Cannot read property 'forEach' of undefined JavascriptTypeError:无法读取未定义 Javascript 的属性“forEach”
【发布时间】:2020-05-28 17:16:56
【问题描述】:

以下是我得到Cannot read property 'forEach' of undefined的代码。

const print2 = function(x, y) {
  console.log(x*y)
}

[1,2,3,4].forEach( x => print2(x, 20) )

让我知道我在这里做错了什么,但如果我这样做了 -

function print2(x, y) {
  console.log(x*y)
}

[1,2,3,4].forEach( x => print2(x, 20) )

这工作正常。

【问题讨论】:

  • @Andreas 你点击“运行代码 sn-p”了吗?
  • 错误信息的确切措辞取决于浏览器。我收到了他在 Chrome 中报告的错误。

标签: javascript node.js ecmascript-6 callback


【解决方案1】:

由于函数后面没有分号,所以 sn-p 被解释为如下:

const print2 = function(x, y) {
  console.log(x*y)
}[1,2,3,4].forEach( x => print2(x, 20) )

这意味着它正在尝试对函数进行索引。在函数之后或数组字面量之前添加分号:

const print2 = function(x, y) {
  console.log(x*y)
};

[1,2,3,4].forEach( x => print2(x, 20) )

const print2 = function(x, y) {
  console.log(x*y)
}

;[1,2,3,4].forEach( x => print2(x, 20) )

更多关于 Javascript 的自动分号插入在这里:What are the rules for JavaScript's automatic semicolon insertion (ASI)?

【讨论】:

  • Wooow..this is a clear case of adding semicolons in your code 尽管它是可选的:)
  • 可能!我个人使用prettier,这使得这些错误非常突出。
【解决方案2】:

变量声明的末尾需要一个分号。

const print2 = function(x, y) {
  console.log(x*y)
};

[1,2,3,4].forEach( x => print2(x, 20) )

没有分号,它被视为

const print2 = function(x, y) {
  console.log(x*y)
}[1,2,3,4].forEach( x => print2(x, 20) )

[1,2,3,4] 被解释为属性访问器,而不是数组,并且逗号运算符返回最后一个值 4。由于该函数没有4 属性,因此返回undefined,然后您尝试调用.forEach()

【讨论】:

    【解决方案3】:

    FunctionExpression 后面缺少分号。

    const print2 = function(x, y) {
      console.log(x*y)
    }
    
    [1,2,3,4].forEach( x => print2(x, 20) )
    

    改成

    const print2 = function(x, y) {
      console.log(x*y)
    };
    
    [1,2,3,4].forEach( x => print2(x, 20) )
    

    它有效。

    【讨论】:

      【解决方案4】:

      我认为,您应该使用像array.forEach(x => print2(x, 20)); 这样的变量,而不是[1,2,3,4].forEach( x => print2(x, 20) )

      例如下面给出的代码:

      let array = [1,2,3,4];
      const print2 = function(x, y) {
          console.log(x*y);
      };
      
      array.forEach( x => print2(x, 20) );
      

      【讨论】:

        【解决方案5】:

        为了记录。 我想补充一下,我在使用该代码时遇到了同样的错误

        var createFiltersList = function() {
                window.filterMenu.forEach(function(text) {     /* <== error here */
                    filterList.push({
                        text: text,
                        expr: text !== ALL ? [DevAV.filterField, "=", text] : null,
                        type: DevAV.filterCaption,
                        custom: false
                    });
                });
        
                var customItems = JSON.parse(window.localStorage.getItem(storageKey));
                Array.prototype.push.apply(filterList, customItems);
            };

        我的问题是我没有为 window.filterMenu 设置任何值,所以 filterMenu 为空,所以 forEach 不适用于空。 一旦将一组值设置为 filterMenu,错误就消失了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-29
          • 2020-03-25
          相关资源
          最近更新 更多