Opera,For In Loop,相同的键出现两次 [英] Opera, For In Loop, Same Key Appears Twice
问题描述
标题说。如果我在HTML集合中使用for循环,我会两次获得
长度。
<!DOCTYPE HTML>
< html lang =" en">
< head>
< title>长度两倍< / title>
< / head>
< body>
< form action =" javascript :;" id =" form1">
< input name =" length" type =" text" />
< / form>
< script type =" text / javascript">
var f = document.forms [0] .elements,a = [];
for(p in f){
a.push(''"' '+ p +''":''+ f [p]);
}
document.write("< xmp>" + a.join (",\ n")+"<" +" / xmp>");
< / script>
< /正文>
< / html>
Opera:
" 0" :[object HTMLInputElement],
" length" :[object HTMLInputElement],
" length" :[object HTMLInputElement],
" item" :function item(){[native code]},
" namedItem" :function namedItem(){[native code]},
" tags" :function tags(){[native code]}
但是,如果名为-length-的元素是
,则长度只会出现两次。否则,长度属性只出现一次,并且具有
" normal"表单控件数量的值。
这里可能发生什么?我怀疑
中的对象可能是带有 - length - 属性的原型,但是无法验证这一点。
hasOwnProperty总是返回false。
加勒特
dhtml写道:
标题说。如果我在HTML集合中使用for循环,我会两次获得
长度。
(在Opera中)
< blockquote class =post_quotes>
>
Garrett
dhtml写道:
<!DOCTYPE HTML>
< html lang =" en">
< head>
* *< title>长度两次< / title>
< / head>
< body>
< form action =" javascript :;" id =" form1">
* * * *< input name =" length" type =" text" />
< / form>
< script type =" text / javascript">
var f = document.forms [0] .elements,a = [];
for(p in f){
* * a.push(''' ;''+ p +''":''+ f [p]);}
document.write("< xmp>" + a.join(" ;,\ n")+"<" +" / xmp>");
< / script>
< / body>
< / html>
Opera:
" 0" :[object HTMLInputElement],
" length" :[object HTMLInputElement],
" length" :[object HTMLInputElement],
" item" :function item(){[native code]},
" namedItem" :function namedItem(){[native code]},
" tags" :function tags(){[native code]}
但是,如果名为-length-的元素是
,则长度只会出现两次。否则,长度属性只出现一次,并且具有
" normal"表单控件数量的值。
这里可能发生什么?我怀疑
中的对象可能是具有 - length - 属性的原型,但是无法验证这一点。
hasOwnProperty总是返回false。
一个是指用户定义的对象
document.forms [0] .elements [''length''](多个-dimensional,可以通过
的属性/方法访问),另一个是语法相同的
宿主对象(但是作为一维的immatricial变量)。
您的测试在我的Opera 9.22.8801中没有显示相同的结果;用户定义的对象似乎覆盖了主机对象。
给出像length这样的名字永远都不错用户定义的对象。
-
Bart
9月2日,5日: 50 * am,dhtml< dhtmlkitc ... @ gmail.comwrote:
dhtml写道:
标题说。如果我在HTML集合中使用for循环,我会两次获得
长度。
(在Opera中)
Garrett
使用您自己的变量/字段的预定义[保留] var名称AVOID ......
Title says it. If I use a for in loop on an HTML collection, I get
length twice.
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>length twice</title>
</head>
<body>
<form action="javascript:;" id="form1">
<input name="length" type="text"/>
</form>
<script type="text/javascript">
var f = document.forms[0].elements, a=[];
for(p in f) {
a.push(''"''+p+''" : '' + f[p]);
}
document.write("<xmp>"+a.join(",\n")+"<"+"/xmp>");
</script>
</body>
</html>
Opera:
"0" : [object HTMLInputElement],
"length" : [object HTMLInputElement],
"length" : [object HTMLInputElement],
"item" : function item() { [native code] },
"namedItem" : function namedItem() { [native code] },
"tags" : function tags() { [native code] }
However, length only appears twice way if the element named -length- is
present. Otherwise, the length property only appears once, and has the
"normal" value of the number of form controls.
What could be happening here? I suspect that there may be an object in
the prototype with a - length - property, but can''t verify this.
hasOwnProperty always returns false.
Garrett
dhtml wrote:Title says it. If I use a for in loop on an HTML collection, I get
length twice.
(in Opera)
>
Garrett
dhtml wrote:
<!DOCTYPE HTML>
<html lang="en">
<head>
* *<title>length twice</title>
</head>
<body>
<form action="javascript:;" id="form1">
* * * * <input name="length" type="text"/>
</form>
<script type="text/javascript">
var f = document.forms[0].elements, a=[];
for(p in f) {
* *a.push(''"''+p+''" : '' + f[p]);}
document.write("<xmp>"+a.join(",\n")+"<"+"/xmp>");
</script>
</body>
</html>
Opera:
"0" : [object HTMLInputElement],
"length" : [object HTMLInputElement],
"length" : [object HTMLInputElement],
"item" : function item() { [native code] },
"namedItem" : function namedItem() { [native code] },
"tags" : function tags() { [native code] }
However, length only appears twice way if the element named -length- is
present. Otherwise, the length property only appears once, and has the
"normal" value of the number of form controls.
What could be happening here? I suspect that there may be an object in
the prototype with a - length - property, but can''t verify this.
hasOwnProperty always returns false.The one refers to your user-defined object
document.forms[0].elements[''length''] (multi-dimensional, accessible by
its properties/methods) and the other to the syntactically identical
host object (but as a one-dimensional immatricial variable).
Your test does not show the same result in my Opera 9.22.8801; there
the user-defined object appears to overwrite the host object.
It is never good to give names like "length" to user-defined objects.
--
Bart
On Sep 2, 5:50*am, dhtml <dhtmlkitc...@gmail.comwrote:dhtml wrote:Title says it. If I use a for in loop on an HTML collection, I get
length twice.
(in Opera)
Garrett
AVOID using predefined [reserved] var names for your own vars/fields...
这篇关于Opera,For In Loop,相同的键出现两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!