for-in JavaScript 语句中的 IE8 错误?
我想我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 IE 中的一个错误,也适用于名为
valueOf
的属性。您可以像这样解决它:
This is a bug in IE, and also applies to properties named
valueOf
.You can work around it like this:
是的,这是一个错误。请参阅此答案。
引用 CMS:
Yes, it is a bug. See this answer.
Quoting CMS: