Closed Bug 352203 Opened 18 years ago Closed 17 years ago

Can't use for-in to iterate over arguments

Categories

(Core :: JavaScript Engine, defect)

1.8 Branch
x86
Linux
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: rockristao, Unassigned)

References

Details

User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1b2) Gecko/20060821 Firefox/2.0b2
Build Identifier: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1b2) Gecko/20060821 Firefox/2.0b2

the arguments array into a function in any spidermonkey don't have a behavior of array,
for example:
for ( i in arguments){
    print(arguments[i]);
}
will not work

Reproducible: Always

Steps to Reproduce:
1.function(x) { for ( i in arguments){
    print(arguments[i]);
    }
}
2.x(1,2,3)
3.

Actual Results:  
none

Expected Results:  
1
2
3
Reporter, do you still see this problem with the latest Firefox 2? If not, can you please close this bug as WORKSFORME. Thanks!
Whiteboard: CLOSEME 07/09
Version: unspecified → 2.0 Branch
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Version: 2.0 Branch → 1.8 Branch
--> Over to the clever chaps at core:javascript engine
According to the JS spec (10.1.8), the args properties attached to the arguments object have the DontEnum property, which means a for...in loop won't work for you.  Try looping from 0 to arguments.length and iterating over those indexes.
Status: UNCONFIRMED → RESOLVED
Closed: 17 years ago
Resolution: --- → INVALID
This is an ECMA-262 botch, and we should fix it in ES4. It shouldn't break any existing code. I'll file a trac ticket at http://bugs.ecmascript-lang.org/.

/be
Yeah, it's actually kind of weird that the DontEnum is specified here, do you recall the justification, Brendan?
It's water way under the dam. The MS guy (bright guy, btw) reversed himself on whether arguments should be an Array or a magic object seemingly of class Object but with magic/lazy getters and setters. I wish I'd fought for Array, but within a day he changed to the magic object, and we were fighting other battles (this was in 1996 or 1997, probably 1997 -- so Netscape was destroying itself at the same time; fun times).

Anyway, the magic object implementation of arguments in IE JScript perhaps was too magic: it didn't want to reify the elements for for-in, or something like that. The JS API has resolve and enumerate hooks for lazy property creation (resolve) and eager for-in reflection (enumerate). So the DontEnum attribute is just a botch in the spec, IMHO.

/be
Summary: problems with arguments array → Can't use for-in to iterate over arguments
Whiteboard: CLOSEME 07/09
You need to log in before you can comment on or make changes to this bug.