Re: Is there a "for...in& quot; in javascript?
kaeli <tiny_one@NOSPA M.comcast.net> writes:
[color=blue]
> I've been unable to find out if javascript supports
> for (var e in obj)
> type of looping syntax.[/color]
Where have you tried looking?
Why did you think of the syntax to begin with? :)
[color=blue]
> Does it?[/color]
Also check ECMA 262 v3, section 12.6.4.
[color=blue]
> If so, is this for DOM browsers only?[/color]
What is a DOM browser? One that supports the W3C DOM {1,2}
specification, or just any DOM?
Probably not, though. It existed in Netscape 2 (i.e., JavaScript 1.0),
the very first browser with Javascript.
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Re: Is there a "for...in& quot; in javascript?
In article <u14hlq97.fsf@h otpop.com>, lrn@hotpop.com enlightened us
with...[color=blue]
> kaeli <tiny_one@NOSPA M.comcast.net> writes:
>[color=green]
> > I've been unable to find out if javascript supports
> > for (var e in obj)
> > type of looping syntax.[/color]
>
> Where have you tried looking?[/color]
Google. :)
The search for such common words as for and in was bringing back a TON
of matches. heh
[color=blue]
> Why did you think of the syntax to begin with? :)[/color]
Java has it.
[color=blue][color=green]
> > Does it?[/color]
>
> Yes.
>
> <URL:http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/stmt.html#10048 15>
> <URL:http://msdn.microsoft. com/library/en-us/script56/html/js56jsstmforin. asp>[/color]
Hey, cool!
[color=blue]
>
> Also check ECMA 262 v3, section 12.6.4.
>[color=green]
> > If so, is this for DOM browsers only?[/color]
>
> What is a DOM browser? One that supports the W3C DOM {1,2}
> specification, or just any DOM?
>[/color]
W3C DOM level 1 was what I had in mind.
Looks like it does. This actually surprises me because I never see it
used (by never, I mean in examples on the web, tutorials, etc) in what I
would think would be a common way - looping through form elements for
validation. I always see stuff like
for (x=0; x<formname.elem ents.length-1; x++)
Seems to me this would be a lot nicer with
for (var e in document.forms["formname"].elements)
Even better, what I wanted it for was looping through a select element
that was dynamically generated (thus, I don't know indexes, only some
possible values).
Simple example:
var mySelect = document.forms["myForm"].elements["mySelect"];
for (var o in mySelect.option s)
{
if (someBoolean) o.selected = true;
alert(o.value+" selected!");
}
--
--
~kaeli~
Who is General Failure and why is he reading my hard disk?
Re: Is there a "for...in& quot; in javascript?
kaeli <tiny_one@NOSPA M.comcast.net> writes:
[color=blue]
> Looks like it does. This actually surprises me because I never see it
> used (by never, I mean in examples on the web, tutorials, etc) in what I
> would think would be a common way - looping through form elements for
> validation. I always see stuff like
> for (x=0; x<formname.elem ents.length-1; x++)
>
> Seems to me this would be a lot nicer with
> for (var e in document.forms["formname"].elements)[/color]
The problem is that you don't know what will be included.
Object properties have a hidden property that says whether they
are enumerable or not. All the properties of Array.prototype , as well
as array lengths, are not enumerable, so doing
for ( var i in arrayRef ) {...}
works. However, in some browsers, the form's elements' "item" property
is enumerable, as are both the named and numbered properties, so
using your code above on the form
<form id="formname" action="">
<input type="text" name="a">
<input type="radio" name="b" value="x1">
<input type="radio" name="b" value="x2">
</form>
will give some of the following properties:
"a", "b", "0", "1", and "2", and a lot more.
Probably not what you had in mind :).
IE's result can be explained by it's desing: The form.elements
reference points to the form element itself.
It works wonders for objects you have made yourself, like ones
you use as hash tables.
[color=blue]
> for (var o in mySelect.option s)[/color]
Same problem. DOM nodes have no standard saying which properties are
enumerable, and it differes between browsers.
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Re: Is there a "for...in& quot; in javascript?
In article <llptlmeh.fsf@h otpop.com>, lrn@hotpop.com enlightened us
with...[color=blue][color=green]
> >
> > Seems to me this would be a lot nicer with
> > for (var e in document.forms["formname"].elements)[/color]
>
> The problem is that you don't know what will be included.[/color]
I'm finding that out. This explains the problem in my other post,
especially what happened with IE.
[color=blue]
> Object properties have a hidden property that says whether they
> are enumerable or not. All the properties of Array.prototype , as well
> as array lengths, are not enumerable, so doing
> for ( var i in arrayRef ) {...}
> works. However, in some browsers, the form's elements' "item" property
> is enumerable, as are both the named and numbered properties, so
> using your code above on the form
> <form id="formname" action="">
> <input type="text" name="a">
> <input type="radio" name="b" value="x1">
> <input type="radio" name="b" value="x2">
> </form>
> will give some of the following properties:
> "a", "b", "0", "1", and "2", and a lot more.
> Probably not what you had in mind :).
>[/color]
No, not at all.
In Java, if I have an object with a property that is an array of objects
(which is what I was thinking a form was), I get the kind of thing I was
expecting.
I wasn't expecting the garbage I got from trying this with a select
element. heh
[color=blue]
> Hmm, let's try:[/color]
<snip>
Damn!
Well, there goes that idea.
*sigh*
[color=blue]
>
> IE's result can be explained by it's desing: The form.elements
> reference points to the form element itself.
>
> It works wonders for objects you have made yourself, like ones
> you use as hash tables.[/color]
And that's what the tutorial showed it being used on.
I rarely uses hashes in JS. I use them in Java, which is where I got the
notion.
Oh well.
Thanks!!
--
--
~kaeli~
Synonym: the word you use in place of a word you can't
spell.
Comment