javascript 使用“for (i=0; ..)”时“分配给未声明的变量”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22850380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"assignment to undeclared variable" when using "for (i=0; ..)"
提问by sgp667
Hey I'm trying to get list of all input fields in a HTML form, but I get following error(in Firebug):
嘿,我正在尝试获取 HTML 表单中所有输入字段的列表,但出现以下错误(在 Firebug 中):
ReferenceError: assignment to undeclared variable i
for (i=0 ; i<inputs.length; i++)
I don't understant how is "i" undeclared because that is that first part of "for" does. This is my formula
我不明白“i”是如何未声明的,因为这是“for”的第一部分。这是我的公式
function listinputs()
{
var form = document.getElementById("wholeform");
var inputs = form.childNodes;
for (i=0 ; i<inputs.length; i++)
{
var string=string + inputs[i].nodeName + "<br>";
var here = document.getElementsByTagName("p");
here.innerHTML(string);
}
}
回答by Sterling Archer
for (var i=0; i<inputs.length; i++)
You need to declare it with var
你需要声明它 var
As T.J said in his answer, since you're using strict mode, an implicit global is not made. That's why an error is thrown.
正如 TJ 在他的回答中所说,由于您使用的是严格模式,因此不会产生隐式全局。这就是抛出错误的原因。
回答by T.J. Crowder
It's not saying i
is unassigned, it's saying it's undeclared. The code never declares an i
variable, but then tries to assign a value to it (in the initialization part of the for
loop). Apparently you're using strict mode (good!), and so the engine is giving you an error rather than creating an implicit global.
不是说i
is unassigned,而是说它是undeclared。代码从不声明i
变量,但随后尝试为其赋值(在for
循环的初始化部分)。显然您正在使用严格模式(很好!),因此引擎给了您一个错误,而不是创建一个隐式的 global。
Declare i
using var
in the function, e.g.:
在函数中声明i
using var
,例如:
function listinputs()
{
var form = document.getElementById("wholeform");
var inputs = form.childNodes;
var i; // <=================================== Here
for (i=0 ; i<inputs.length; i++)
{
string=string + inputs[i].nodeName + "<br>";
here = document.getElementsByTagName("p");
here.innerHTML(string);
}
}
Side note: In ES6, when it arrives, if you want you can use let
and scope i
to the for
statement only. But for now, use var
.
旁注:在 ES6 中,当它到达时,如果你愿意,你可以只使用let
和作用域i
到for
语句。但是现在,使用var
.
回答by Sankar_k
ERROR Message
错误信息
ReferenceError: assignment to undeclared variable "x" (Firefox)
ReferenceError: "x" is not defined (Chrome)
ReferenceError: Variable undefined in strict mode (Edge)
ReferenceError: 赋值给未声明的变量 "x" (Firefox)
ReferenceError: "x" is not defined (Chrome)
ReferenceError: Variable undefined in strict mode (Edge)
Invalid cases
In this case, the variable "bar" is an undeclared variable.
无效的情况
在这种情况下,变量“ bar”是一个未声明的变量。
function foo() {
'use strict';
bar = true;
}
foo(); // ReferenceError: assignment to undeclared variable bar
Valid cases
To make "bar" a declared variable, you can add the var keyword in front of it.
有效情况
要使“ bar”成为声明的变量,可以在其前面添加 var 关键字。
function foo() {
'use strict';
var bar = true;
}
foo();