Closed
Bug 341426
Opened 19 years ago
Closed 19 years ago
new Function() collection contains prototype object in for(var i in obj) loop
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
RESOLVED
INVALID
People
(Reporter: dwilcox, Unassigned)
Details
Attachments
(1 file)
1.82 KB,
text/html
|
Details |
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4
Iterating over an object via for i in obj loop, when the object was created using the new Function() constructor shows one prototype object that exists, even if the object is unpopulated. So an empty object will be reported to have member properties.
e.g.:
var SomeFunction = new Function()
var out = '';
for(var i in obj){
out += i
out += ' = '
out += obj[i]
out += '\n'
}
alert(out)
Would produce:
prototype = [object Object]
This is new in Firefox 1.5.x+. Does not happen in IE, Safari, Firefox 1.0.7-.
I'll attach a working demonstration/test case.
Reproducible: Always
Steps to Reproduce:
<html>
<head>
<script type="text/javascript">
var SomeFunction = new Function()
// would also happen with the following code:
// var SomeFunction = function myFunction(){}
var SomeObject = new Object()
var SomeArray = new Array()
function addParameter(obj, name, value){
obj[name] = value
}
function describeSomeObject(obj, locId){
var out = '';
for(var i in obj){
out += i
out += ' = '
out += obj[i]
out += '\n'
}
/* The following are interesting but unnecesary:
out += obj.toSource()
out += '\n'
out += obj.valueOf() */
document.getElementById(locId).value=out
}
function addSampleParamsAndDescribe(obj, locId){
addParameter(obj, "alpha", "One")
addParameter(obj, "beta", "Two")
addParameter(obj, "gamma", "Three")
describeSomeObject(obj, locId);
}
function populateFields(){
addSampleParamsAndDescribe(SomeFunction, 'someFunctionBlock')
addSampleParamsAndDescribe(SomeObject, 'someObjectBlock')
addSampleParamsAndDescribe(SomeArray, 'someArrayBlock')
}
function clearFields(){
document.getElementById('someFunctionBlock').value=''
document.getElementById('someObjectBlock').value=''
document.getElementById('someArrayBlock').value=''
}
function init(){
clearFields()
populateFields()
}
</script>
</head>
<body onload="init()">
<p><strong>SomeFunction = new Function()<br /></strong><textarea cols=30 rows=4 id="someFunctionBlock"></textarea></p>
<p><strong>SomeObject = new Object()<br /></strong><textarea cols=30 rows=4 id="someObjectBlock"></textarea></p>
<p><strong>SomeArray = new Array()<br /></strong><textarea cols=30 rows=4 id="someArrayBlock"></textarea></p>
<span style="cursor:pointer;" onclick="populateFields()">[ Populate ]</span> <span style="cursor:pointer;" onclick="clearFields()">[ Clear Results ]</span>
</body>
</html>
Actual Results:
alpha = One
beta = Two
gamma = Three
prototype = [object Object]
Expected Results:
alpha = One
beta = Two
gamma = Three
Workaround:
Don't use new Function() to initialize collections or objects whose contents are going to be iterated over using for i in obj. Use new Object() or new Array() instead.
Reporter | ||
Comment 1•19 years ago
|
||
Reporter | ||
Updated•19 years ago
|
Summary: new Function() collection contains prototype object in var i in loop → new Function() collection contains prototype object in for(var i in obj) loop
Updated•19 years ago
|
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Version: unspecified → Trunk
Comment 2•19 years ago
|
||
Invalid, see bug 299642
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → INVALID
Reporter | ||
Comment 3•19 years ago
|
||
I did do lots of searching, even on closed bugs. Sorry. It never occurred to me to check the ECMA spec. Next time ...
Comment 4•19 years ago
|
||
This is probably just an old bug in Netscape 2 that got frozen by ECMA-262 Edition 1. It'll be hard to justify changing for ES4, but perhaps worth the trouble.
/be
Reporter | ||
Comment 5•19 years ago
|
||
Just to clarify, this didn't occur before Ff 1.5.x, nor does it occur in the other browsers we test against.
Here's the reference from 299642:
According to ECMAscript spec section 13.2 and 15.3.5.2 the
"prototype"-property of Javascript functions is *not* "DontEnum", and
should hence be visible when doing a "for .. in" of the function object.
You need to log in
before you can comment on or make changes to this bug.
Description
•