更新时间:2023-02-11 08:48:55
8月27日下午1:55,Phat G5(G3) < nob ... @ noone.comwrote:
我正在为向Array类添加一个新方法而烦恼。它可以在FF中使用
罚款,但在Safari中则不行。我不知道在IE中。我还没有得到
远。
Array.prototype.find = function(item){for(var i in this)
if(item == this(i)
别忘了手动将代码包装成大约70个字符到
允许自动换行。
IE经常混淆()和[],也许你发现Firefox尝试
来补贴(或者不......只是一个猜测。
尝试:
if(item == this [i])
-
Rob
文章 11 ********************** @ r23g2000prd.googlegroups .com ,RobG在
rg***@iinet.net.au 上写道8 / 27/07 1:52 AM:
8月27日下午1点55分,Phat G5(G3)< nob ... @ noone .comwrote:
>我正在为向Array类添加一个新方法而烦恼。它在FF中工作得很好但在Safari中却没有。我不知道在IE中。我还没有得到它。
Array.prototype.find = function(item){for(var i in this)
if(item == this(i) )
别忘了手动将代码包装在大约70个字符处
允许自动换行。
IE经常让[]与[]混淆,也许你已经发现Firefox尝试
来补贴(或者不是......只是猜测)。
尝试:
if(item == this [i])
-
Rob
这是我修复过的错误,在发送之前忘了发帖。抱歉。这里
是基本的关于我在做什么的想法。
Array.prototype.find = function(item){for(var i in this)
if(item ==这[i])
返回(true)
返回(false);
};
var groups = new Array(0);
var dials = x.form.getElementsByTagName(&quo t;输入)
for(var i in dials)
if(dials [i] .type ==" radio" &安培;&安培; !groups.find(dials [i] .name))
groups = groups.concat(new Array(dials [i] .name))
我试图通过一个表单的无线电拨号,并创建一个
阵列只是收音机拨号组/名称,然后能够像theForm一样在
a时解析组。元素[基团[I]] [j]的.checked。检查
组的输出会产生一个空数组。出于测试目的,我只采用了上面的
方法并进行了快速测试。
var x = new Array(" zz");
alert(x.find(" zz"))每次在Safari和IE6 + 7中都返回false,但在
Firefox中是真的。
它会出现原型寄存器,但它不会识别它自己的
属性,并且永远不会通过它们进行迭代,最终命中返回false。
从for ...更改循环到常规for循环没有区别。任何
其他建议?
谢谢
-S
8月27日上午6:45,Phat G5(G3) < nob ... @ noone.comwrote:
var x = new Array(" zz");
alert(x .find(" zz"))每次都在Safari和IE6 + 7中返回false,但在
Firefox中是真的。
它会出现原型寄存器但是它没有认识到它自己的
属性并且从不通过它们进行迭代并最终命中返回false。
从for..in循环变为a常规for循环没有区别。任何
其他建议?
嗯,是的:
1.使用[],而不是Array构造函数。
2.永远不要使用for .. in语法迭代数组,
*尤其是*如果你正在扩展Array.prototype。使用传统的
代替(var i = 0,len = this.length; i< len; i ++)循环。
3.不要使用括号返回关键字。
4.总是用分号结束行。
5.如果你遇到循环和/或if语句的问题,请将
$ b包起来$ b他们的花括号{}。
通过JSLint运行你的代码( http://www.jslint.com/lint.html )
一些有启发性的建议。
因此,您的代码变为:
Array.prototype.find = function(item){
for(var i = 0; i< this.length ; i ++){
if(this [i] == item){
返回true;
}
}
返回false;
};
这样测试[''a'',''b' ',''c'']。在IE中找到(''b'')=== true。
但更好的技巧是实现JS 1.6'的
if(typeof Array.prototype.indexOf!=''function''){
Array.prototype.indexOf = function(item){
// ..在这里实现
//参见 http://developer.mozilla.org/en/docs...:Array:indexOf
}
}
因为它已经存在于大多数浏览器中但是IE浏览器。
-David
I was toying around with adding a new method to the Array class. It works
fine in FF but not in Safari. I have no idea in IE. I have not gotten that
far yet.
Array.prototype.find = function(item) { for(var i in this )
if( item == this(i) )
return( true )
return( false );
};
Any suggestions?
Thanks, S
On Aug 27, 1:55 pm, "Phat G5 (G3)" <nob...@noone.comwrote:I was toying around with adding a new method to the Array class. It works
fine in FF but not in Safari. I have no idea in IE. I have not gotten that
far yet.
Array.prototype.find = function(item) { for(var i in this )
if( item == this(i)Don''t forget to manually wrap your code at about 70 characters to
allow for auto-wrapping.
IE often confuses () with [], maybe you have discovered Firefox trying
to make allowances for that (or not... just a guess).
Try:
if( item == this[i] )
--
Rob
in article 11**********************@r23g2000prd.googlegroups. com, RobG at
rg***@iinet.net.au wrote on 8/27/07 1:52 AM:On Aug 27, 1:55 pm, "Phat G5 (G3)" <nob...@noone.comwrote:>I was toying around with adding a new method to the Array class. It works
fine in FF but not in Safari. I have no idea in IE. I have not gotten that
far yet.
Array.prototype.find = function(item) { for(var i in this )
if( item == this(i)
Don''t forget to manually wrap your code at about 70 characters to
allow for auto-wrapping.
IE often confuses () with [], maybe you have discovered Firefox trying
to make allowances for that (or not... just a guess).
Try:
if( item == this[i] )
--
RobThat was a typo I had fixed and forgot to post before sending. Sorry. Here
is a basic idea of what I was doing.
Array.prototype.find = function(item) { for( var i in this )
if( item == this[i] )
return( true )
return( false );
};
var groups = new Array(0);
var dials = x.form.getElementsByTagName("input")
for( var i in dials )
if( dials[i].type == "radio" && !groups.find(dials[i].name) )
groups = groups.concat(new Array(dials[i].name))
I was trying to go thru the radio dials of a form and create an array of
just the radio dial groups/names and then be able to parse the groups one at
a time like theForm.elements[groups[i]][j].checked. Checking the output of
groups yields an empty array. For testing purposes I just took the above
method and did a quick test.
var x = new Array("zz");
alert(x.find("zz")) returns false every time in Safari and IE6+7 but true in
Firefox.
It would appear the prototype registers but it''s not recognizing it''s own
attributes and never iterates thru them and ultimately hits return false.
Changing from a for..in loop to a regular for loop makes no difference. Any
other suggestions?
Thanks
-S
On Aug 27, 6:45 am, "Phat G5 (G3)" <nob...@noone.comwrote:var x = new Array("zz");
alert(x.find("zz")) returns false every time in Safari and IE6+7 but true in
Firefox.
It would appear the prototype registers but it''s not recognizing it''s own
attributes and never iterates thru them and ultimately hits return false.
Changing from a for..in loop to a regular for loop makes no difference. Any
other suggestions?Well, yes:
1. Use [], not the Array constructor.
2. Never, ever use the for .. in syntax to iterate through an Array,
*especially* if you''re extending Array.prototype. Use a conventional
for (var i=0, len=this.length; i<len; i++) loop instead.
3. Don''t use parentheses with the "return" keyword.
4. Always end lines in semicolons.
5. If you''re having trouble with loops and/or if statements, wrap
their blocks in curly braces {}.
Run your code through JSLint (http://www.jslint.com/lint.html) for
some enlightening suggestions.
Thus, your code becomes:
Array.prototype.find = function (item) {
for (var i=0; i<this.length; i++) {
if (this[i] == item) {
return true;
}
}
return false;
};
such that the test [''a'', ''b'', ''c''].find(''b'') === true in IE.
But a better technique would be to implement JS 1.6''s
if (typeof Array.prototype.indexOf != ''function'') {
Array.prototype.indexOf = function (item) {
// .. implementation here
// see http://developer.mozilla.org/en/docs...:Array:indexOf
}
}
as it''s already there on most every browser but IE.
-David