Closed Bug 12951 Opened 25 years ago Closed 25 years ago

var k in object, k should always be of type string ..

Categories

(Core :: JavaScript Engine, defect, P3)

x86
Linux
defect

Tracking

()

RESOLVED FIXED

People

(Reporter: ramky, Assigned: rogerl)

Details

Please Look at the following ...

<html>
<head>
<script>
function handleOnLoad()

    var params = new Array();
    params[0] = 0;
    params[1] = 1;
    params["Hello"] = "Hello";
    var newwin = window.open();
    newwin.document.open("text/html");
    for(var i in params) {
        newwin.document.writeln("typeof(" + i + ") is " + typeof(i) +
'<br>');
    }
    newwin.document.close();

</script>
</head>
<body onLoad="handleOnLoad();">
</body>
</html>

JavaScript, the Definitive Guide by David Flanagan says that when
you say (var i in array), i will always be string. I don't know what
ECMA standard says about this.

Under netscape & Mozilla M9 typeof(i) is number first two times !!

Under IE, it is always of type string.

Workaround is pretty simple, but from a purist's point of view, it is
 non compliant :)

Regards,
ramky.

Subject:
          Re: var k in array problem ...
      Date:
          Tue, 31 Aug 1999 23:01:19 -0700
      From:
          Brendan Eich <brendan@meer.net>
       To:
          Ramakrishna Reddy Kandula <ramky@desktop.com>
 Newsgroups:
          netscape.public.mozilla.jseng
 References:
          1




> Under netscape & Mozilla M9 typeof(i) is number first two times !!
>
> Under IE, it is always of type string.
>
> Workaround is pretty simple, but from a purist's point of view, it is
>  non compliant :)

Non-compliant with IE, or with ECMA?  We don't care about the former, as
you might surmise.

ECMA's a little underspecified here, but if you squint at "property
identifier" and its definition, you'll want a string every time, even for
non-negative integer identifiers.  So file a bug against JavaScript,
assigned to mccabe@netscape.com.  Thanks,

/be
looks like it's more general and applies to indexed properties of any object

js> o = {1:"boo", foo:"bar"}
[object Object]
js> for ( var p in o ) { print( p +":"+typeof p ); }
1:number
foo:string

this follows from 12.6.4 (part on for var in Expression), steps 6-8 in the
latest draft.
Roger, could you peek at this?
Assignee: mccabe → rogerl
Status: NEW → ASSIGNED
There's code in jsinterp to do this conversion, but it's only being executed
when :
(JSVERSION_1_0 <= cx->version && cx->version <= JSVERSION_1_1)

I suspect this should be 'cx->version != JSVERSION_1_2' instead. I'll check that
in.
Status: ASSIGNED → RESOLVED
Closed: 25 years ago
Resolution: --- → FIXED
checked in.
You need to log in before you can comment on or make changes to this bug.