×
  • Web前端首页
  • Js&JQuery
  • 前端报错信息“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”的处理方案

作者:Terry2021.01.05来源:Web前端之家浏览:4487评论:0
关键词:js

分享下前端报错信息“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 循环形式

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 的偏移量,在这种情况下,可以在循环体中进行添加。

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

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/js20210105a1.html

网友评论文明上网理性发言 已有0人参与

发表评论: