R
RobG
It seems that IE always makes the toString property of objects
DontEnum, even when it shouldn't be. For example, the following
function should list all the enumerable properties of obj. Since the
script assigns a toString property, it should be enumerable and be in
the returned properties list:
var obj = {toString: 'toString'};
function getProperties(obj) {
var properties = [];
for (var p in obj) {
properties.push(p);
}
return properties;
}
alert(getProperties(obj).length);
Firefox shows 1, IE shows 0.
Changing the above function to:
function getProperties(obj) {
function g(obj) {
var pr = [];
for (var p in obj) {
pr.push(p);
}
return pr;
}
var properties = g(obj);
if (g({toString:'toString'}).length == 0 &&
typeof obj.toString != 'undefined') {
properties.push('toString');
}
return properties;
}
Is a kind of fix but it also means that toString will be listed as an
object property in browsers like IE where it shouldn't:
var obj2 = {fred:'fred'};
alert(getProperties(obj2).length)
Shows 1 in Firefox but 2 in IE.
Calling obj.propertyIsEnumerable('toString') doesn't help - IE
accurately reports "false" always.
Is there any way of reliably telling in IE whether the toString
property should be enumerable or not?
DontEnum, even when it shouldn't be. For example, the following
function should list all the enumerable properties of obj. Since the
script assigns a toString property, it should be enumerable and be in
the returned properties list:
var obj = {toString: 'toString'};
function getProperties(obj) {
var properties = [];
for (var p in obj) {
properties.push(p);
}
return properties;
}
alert(getProperties(obj).length);
Firefox shows 1, IE shows 0.
Changing the above function to:
function getProperties(obj) {
function g(obj) {
var pr = [];
for (var p in obj) {
pr.push(p);
}
return pr;
}
var properties = g(obj);
if (g({toString:'toString'}).length == 0 &&
typeof obj.toString != 'undefined') {
properties.push('toString');
}
return properties;
}
Is a kind of fix but it also means that toString will be listed as an
object property in browsers like IE where it shouldn't:
var obj2 = {fred:'fred'};
alert(getProperties(obj2).length)
Shows 1 in Firefox but 2 in IE.
Calling obj.propertyIsEnumerable('toString') doesn't help - IE
accurately reports "false" always.
Is there any way of reliably telling in IE whether the toString
property should be enumerable or not?