for-in JavaScript 语句中的 IE8 错误?

发布于 2024-09-19 07:14:19 字数 1176 浏览 2 评论 0原文

我想我在 IE (IE8) 处理 for-in javascript 语句时发现了一个错误。经过几个小时将其归结为一个小示例后,看起来 IE 专门跳过了 for-in 循环中名为“toString”的任何属性 - 无论它是在原型中还是对象的“自己的属性”。

我已将我的测试代码放在这里

function countProps(obj) {
    var c = 0;
    for (var prop in obj) {
        c++;
    }
    return c;
}

var obj = {
    toString: function() {
        return "hello";
    }
};

function test() {
    var o = "";
    var d = document.getElementById('output');

    o += "<br/>obj.hasOwnProperty('toString') == " + obj.hasOwnProperty('toString');
    o += "<br/>countProps(obj) = " + countProps(obj);
    o += "<br/>obj.toString() = " + obj.toString();

    d.innerHTML = o;
}

这应该会产生:

obj.hasOwnProperty('toString') == true
countProps(obj) = 1
obj.toString() = hello

但在 IE 中,我得到:

obj.hasOwnProperty('toString') == true
countProps(obj) = 0
obj.toString() = hello

任何名为“toString”的属性的特殊大小写都会对我尝试将方法复制到 Function.prototype 的代码造成严重破坏 - 我的自定义 toString 函数总是被跳过。

有人知道解决方法吗?这是某种仅在怪癖模式下的行为——还是只是一个BUG?

I think I've found a bug in IE's (IE8) handling for the for-in javascript statement. After several hours of boiling this down to a small example, it looks like IE specifically skips any property called "toString" in a for-in loop - regardless of whether it is in a prototype or is an "Own Property" of the object.

I've placed my test code here:

function countProps(obj) {
    var c = 0;
    for (var prop in obj) {
        c++;
    }
    return c;
}

var obj = {
    toString: function() {
        return "hello";
    }
};

function test() {
    var o = "";
    var d = document.getElementById('output');

    o += "<br/>obj.hasOwnProperty('toString') == " + obj.hasOwnProperty('toString');
    o += "<br/>countProps(obj) = " + countProps(obj);
    o += "<br/>obj.toString() = " + obj.toString();

    d.innerHTML = o;
}

This should produce:

obj.hasOwnProperty('toString') == true
countProps(obj) = 1
obj.toString() = hello

but in IE, I'm getting:

obj.hasOwnProperty('toString') == true
countProps(obj) = 0
obj.toString() = hello

This special casing of any property called 'toString' is wrecking havoc with my code that tries to copy methods into a Function.prototype - my custom toString function is always skipped.

Does anyone know a work-around? Is this some sort of quirks-mode only behavior - or just a BUG?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-09-26 07:14:21

这是 IE 中的一个错误,也适用于名为 valueOf 的属性。

您可以像这样解决它:

if(obj.toString !== Object.prototype.toString || obj.hasOwnProperty("toString"))
    //Handle it
if(obj.valueOf !== Object.prototype.valueOf || obj.hasOwnProperty("valueOf"))
    //Handle it

This is a bug in IE, and also applies to properties named valueOf.

You can work around it like this:

if(obj.toString !== Object.prototype.toString || obj.hasOwnProperty("toString"))
    //Handle it
if(obj.valueOf !== Object.prototype.valueOf || obj.hasOwnProperty("valueOf"))
    //Handle it
聊慰 2024-09-26 07:14:20

是的,这是一个错误。请参阅此答案

引用 CMS

另一个众所周知的 JScript bug 是“DontEnum Bug”,如果一个对象在其作用域链中包含一个不可枚举的属性(具有 { DontEnum } 属性),如果该属性在其他对象上被隐藏,它将保留作为不可枚举的,例如:

var dontEnumBug = {toString:'foo'}.propertyIsEnumerable('toString');

它在 IE 上的计算结果为 false,这会在使用 for-in 语句时导致问题,因为属性不会被访问。

Yes, it is a bug. See this answer.

Quoting CMS:

Another well known JScript bug is the "DontEnum Bug", if an object in its scope chain contains a property that is not enumerable (has the { DontEnum } attribute), if the property is shadowed on other object, it will stay as non-enumerable, for example:

var dontEnumBug = {toString:'foo'}.propertyIsEnumerable('toString');

It will evaluate to false on IE, this causes problems when using the for-in statement, because the properties will not be visited.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文