Is there a "for...in" in javascript?

L

Lasse Reichstein Nielsen

kaeli said:
I've been unable to find out if javascript supports
for (var e in obj)
type of looping syntax.

Where have you tried looking?
Why did you think of the syntax to begin with? :)

Yes.

<URL:http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/stmt.html#1004815>
<URL:http://msdn.microsoft.com/library/en-us/script56/html/js56jsstmforin.asp>

Also check ECMA 262 v3, section 12.6.4.
If so, is this for DOM browsers only?

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
 
K

kaeli

[email protected] enlightened us said:
Where have you tried looking?

Google. :)
The search for such common words as for and in was bringing back a TON
of matches. heh
Why did you think of the syntax to begin with? :)

Java has it.

Hey, cool!
Also check ECMA 262 v3, section 12.6.4.


What is a DOM browser? One that supports the W3C DOM {1,2}
specification, or just any DOM?

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.elements.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.options)
{
if (someBoolean) o.selected = true;
alert(o.value+" selected!");
}


--
 
L

Lasse Reichstein Nielsen

kaeli said:
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.elements.length-1; x++)

Seems to me this would be a lot nicer with
for (var e in document.forms["formname"].elements)

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 :).

Hmm, let's try:
Opera 7 gives: item, tags, namedItem
Mozilla gives: length, item, namedItem
Netscape 4 gives: a, 0, 1, b, 2, length, name, elements, method,
action, encoding, target
IE 6 gives (brace yourself): language, scrollHeight, isTextEdit,
currentStyle, document, onmouseup, oncontextmenu, isMultiLine,
clientHeight, onrowexit, onbeforepaste, onactivate, scrollLeft,
lang, onmousemove, onmove, onselectstart, parentTextEdit,
oncontrolselect, canHaveHTML, onkeypress, oncut, onrowenter,
onmousedown, onpaste, className, id, onreadystatechange,
onbeforedeactivate, hideFocus, dir, isContentEditable, onkeydown,
clientWidth, onlosecapture, parentElement, ondrag, ondragstart,
oncellchange, recordNumber, onfilterchange, onrowsinserted,
ondatasetcomplete, onmousewheel, ondragenter, onblur, onresizeend,
onerrorupdate, onbeforecopy, ondblclick, scopeName, onkeyup,
onresizestart, onmouseover, onmouseleave, outerText, innerText,
onmoveend, tagName, title, offsetWidth, onresize, contentEditable,
runtimeStyle, filters, ondrop, onpage, onrowsdelete, tagUrn,
offsetLeft, clientTop, style, onfocusout, clientLeft,
ondatasetchanged, canHaveChildren, ondeactivate, isDisabled,
onpropertychange, ondragover, onhelp, ondragend, onbeforeeditfocus,
disabled, onfocus, behaviorUrns, accessKey, onscroll,
onbeforeactivate, onbeforecut, readyState, all, sourceIndex,
onclick, scrollTop, oncopy, onfocusin, tabIndex, onbeforeupdate,
outerHTML, innerHTML, ondataavailable, offsetHeight, onmovestart,
onmouseout, scrollWidth, offsetTop, onmouseenter, onlayoutcomplete,
offsetParent, onafterupdate, ondragleave, children, parentNode,
nodeValue, name, length, onreset, onsubmit, lastChild, elements,
attributes, acceptCharset, action, method, nodeType, target,
previousSibling, ownerDocument, nodeName, childNodes, nextSibling,
firstChild, encoding, a, b, b
(yes, b is there twice!)

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.
for (var o in mySelect.options)

Same problem. DOM nodes have no standard saying which properties are
enumerable, and it differes between browsers.

/L
 
K

kaeli

[email protected] enlightened us said:
Seems to me this would be a lot nicer with
for (var e in document.forms["formname"].elements)

The problem is that you don't know what will be included.

I'm finding that out. This explains the problem in my other post,
especially what happened with IE.
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 :).

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
Hmm, let's try:
<snip>

Damn!

Well, there goes that idea.
*sigh*
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.

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.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top