问题描述
我一直在尝试在 Internet Explorer 中调试一些 js,但我无法弄清楚这一点.这是导致错误的行:
I've been trying to debug some js in Internet Explorer, and I can't figure this one out. Here's the line that is causing the error:
var numberOfColumns = Object.keys(value).length;
错误是...
Message: Object doesn't support this property or method
Line: 640
Char: 5
Code: 0
URI: xxx
起初我认为它与 Object.keys(value).length;
属性有关,但奇怪的是(无论如何对我来说),错误在字符 5,这是变量名的开头.
At first I thought it had something to do with the Object.keys(value).length;
property, but strangely (for me anyways), the error is at char 5, which is the beginning of the variable name.
无论如何,我不知道发生了什么或如何解决它.另外,如果我替换:
Anyways, I have no idea what's going on or how to fix it. Also, if I replace:
var numberOfColumns = Object.keys(value).length;
随着...
var numberOfColumns = 9; // troubleshooting
错误仍然存在.请帮忙.
The error persists. Please help.
更新
jsFiddle 添加了
jsFiddle added
推荐答案
keys
属性在 IE >= 9 中受支持.您可能正在早期版本中测试它.一个简单的解决方法是:
The keys
property is supported in IE >= 9. You are probably testing it in an earlier version. A simple workaround is:
var length = 0;
for(var prop in data){
if(data.hasOwnProperty(prop))
length++;
}
这是一个演示:http://jsfiddle.net/vKr8a/
有关详细信息,请参阅此兼容性表:
See this compatibility table for more info:
http://kangax.github.com/es5-compat-table/
这篇关于IE 问题 - 不支持 Object.keys(value).length的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!