Javascript TypeError:无法将未定义转换为对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18192162/
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
Javascript TypeError: can't convert undefined to object
提问by Robin93K
var horizont, vertikal = new Array ()
for (var i=0; i < 9; i++)
{
horizont[i] = new Array ();
vertikal[i] = new Array ()
}
That's what the console told me:
这就是控制台告诉我的:
TypeError: can't convert undefined to object
类型错误:无法将未定义转换为对象
horizont[i] = new Array ();
(if I would erase it from the code he says the same with vertikal )
(如果我想从代码中删除它,他对 vertikal 说同样的话)
except from some other empty strings getiing born it's the beginning of my code... where is the mistake? Is it so ovious that I don't see it?
除了其他一些空字符串的诞生之外,它是我代码的开始......错误在哪里?是不是太明显了我没看到?
回答by epascarello
The error is because you did not define horizont
as an Array. You are using a comma to separate your variable so it is undefined. It does not use the new Array()
from vertikal.
错误是因为您没有定义horizont
为数组。您正在使用逗号分隔变量,因此它是未定义的。它不使用new Array()
from vertikal。
If you take your code
如果你拿你的代码
var horizont, vertikal = new Array ()
And write it out to use multiple variable, the error would pop out.
并写出来使用多个变量,错误会弹出。
var horizont;
var vertikal = new Array();
You need to specify both as Arrays.
您需要将两者都指定为数组。
var horizont = [],
vertikal = [];