Javascript 对于in-loop无法设置未定义的属性
Javascript 对于in-loop无法设置未定义的属性,javascript,for-loop,properties,undefined,console.log,Javascript,For Loop,Properties,Undefined,Console.log,var table=document.querySelectorAll(“.number”); 函数seppuku(){ 对于(让我在表中){ 表[i].style.color=“红色”; } } seppuku() 1 2. 3使用让i进入表将i的最后一个元素作为长度,因此结果是表['length'].style.color=“red”不正确,因为没有元素表['length']并返回未定义的。更好的方法是在表的长度中循环,它将是0、1和2 var table=document.querySe
var table=document.querySelectorAll(“.number”);
函数seppuku(){
对于(让我在表中){
表[i].style.color=“红色”;
}
}
seppuku()代码>
1
2.
3
使用让i进入表
将i的最后一个元素作为长度
,因此结果是表['length'].style.color=“red”
不正确,因为没有元素表['length']
并返回未定义的
。更好的方法是在表的长度中循环,它将是0、1和2
var table=document.querySelectorAll(“.number”);
函数seppuku(){
对于(var i=0;i使用let i in table
将i
的最后一个元素设置为length
,因此结果为table['length'].style.color=“red”
这是不正确的,因为没有元素table['length']
并返回未定义的
。更好的方法是通过表
的长度
,该长度为0、1和2
var table=document.querySelectorAll(“.number”);
函数seppuku(){
for(var i=0;i您在
语句中使用了
for…,该语句迭代对象的可枚举属性。在本例中,表
对象包含以下可枚举属性
{
"0": <div class="numbers">1</div>,
"1": <div class="numbers">2</div>,
"2": <div class="numbers">3</div>,
"length": 3,
"item": function item() { [native code] },
"entries": function entries() { [native code] },
"forEach": function forEach() { [native code] },
"keys": function keys() { [native code] },
"values": function values() { [native code] }
}
1
2.
3
您在
语句中对…使用了,该语句迭代对象的可枚举属性。在这种情况下,表
对象包含以下可枚举属性
{
"0": <div class="numbers">1</div>,
"1": <div class="numbers">2</div>,
"2": <div class="numbers">3</div>,
"length": 3,
"item": function item() { [native code] },
"entries": function entries() { [native code] },
"forEach": function forEach() { [native code] },
"keys": function keys() { [native code] },
"values": function values() { [native code] }
}
1
2.
3
在中使用简单的for循环,而不是for
1.
2.
3.
函数seppuku(){
var table=document.queryselectoral(“.number”);
for(设i=0;i使用简单的for循环,而不是for..in
1.
2.
3.
函数seppuku(){
var table=document.queryselectoral(“.number”);
for(让i=0;i-。而不是for in-loop使用for-loop尝试console.log(table)
,你就会明白你的循环在迭代什么,因为document.queryselectoral(.numbers”)
的结果是一个类似数组的对象,如果你想使用for循环,你必须转换成arrayvar table=document.querySelectorAll(.numbers));
table=Array.prototype.slice.call(table)
请参见-。请尝试console.log(table)
而不是for in-loop,这样您就可以了解循环的迭代内容,因为document.querySelectorAll(.numbers)的结果
这是一个类似数组的对象,如果你想使用for循环,你必须转换为arrayvar table=document.queryselectoral(“.numbers”);
table=array.prototype.slice.call(table)
谢谢。我只是想深入了解ES6(我知道典型的“for循环”方法)。但是,是的,我肯定会使用工作方法;)谢谢。我只是想深入了解ES6(我知道典型的“for-loop”方法)。但是,是的,我肯定会使用工作方法;)谢谢。我只是想深入了解ES6(我知道典型的“for-loop”方法)。但是,是的,我肯定会使用工作方法;)我添加了更多的解释。请让我知道它是否对您有帮助谢谢。我只是想深入了解ES6(我知道典型的“for循环”方法)。但是,是的,我肯定会使用工作方法;)我添加了更多的解释。请让我知道它是否对您有帮助