Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. JavaScript
  3. JavaScript 参考
  4. JavaScript 错误参考
  5. SyntaxError: a declaration in the head of a for-of loop can't have an initializer

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

SyntaxError: a declaration in the head of a for-of loop can't have an initializer

错误信息

SyntaxError: a declaration in the head of a for-of loop can't have an initializer (Firefox)SyntaxError: for-of loop variable declaration may not have an initializer. (Chrome)

错误类型

SyntaxError

哪里出错了?

for...of 循环的头部包含有初始化表达式。也就是对一个变量进行声明并赋值 |for (var i = 0 of iterable)|。这在 for-of 循环中是被禁止的。你想要的可能是允许包含初始化器的for 循环形式。

示例

非法的for-of 循环形式

js
let iterable = [10, 20, 30];for (let value = 50 of iterable) {  console.log(value);}// SyntaxError: a declaration in the head of a for-of loop can't// have an initializer

合法的for-of 循环形式

需要将初始化器 (value = 50) 从for-of 循环的头部移除。或许你的本意是给每个值添加 50 的偏移量,在这种情况下,可以在循环体中进行添加。

js
let iterable = [10, 20, 30];for (let value of iterable) {  value += 50;  console.log(value);}// 60// 70// 80

相关内容

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp