javascript IE 问题 - 不支持 Object.keys(value).length

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13723805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:37:02  来源:igfitidea点击:

IE issue - Object.keys(value).length not supported

javascriptjqueryinternet-explorer

提问by veryConfused

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:

我一直在尝试在 Internet Explorer 中调试一些 js,但我无法弄清楚这一点。这是导致错误的行:

var numberOfColumns = Object.keys(value).length;

And the error is...

而错误是...

Message: Object doesn't support this property or method
Line: 640
Char: 5
Code: 0
URI: xxx

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.

起初我认为它与Object.keys(value).length;属性有关,但奇怪的是(无论如何对我来说),错误出现在 char 5,这是变量名的开头。

Anyways, I have no idea what's going on or how to fix it. Also, if I replace:

无论如何,我不知道发生了什么或如何解决它。另外,如果我替换:

var numberOfColumns = Object.keys(value).length;

With ...

和 ...

var numberOfColumns = 9; // troubleshooting

The error persists. Please help.

错误仍然存​​在。请帮忙。

Update

更新

jsFiddle added

添加了 jsFiddle

http://jsfiddle.net/4Rab7/

http://jsfiddle.net/4Rab7/

回答by Asad Saeeduddin

The keysproperty is supported in IE >= 9. You are probably testing it in an earlier version. A simple workaround is:

keys属性在 IE >= 9 中受支持。您可能正在早期版本中测试它。一个简单的解决方法是:

var length = 0;
for(var prop in data){
    if(data.hasOwnProperty(prop))
        length++;
}

Here is a demonstration: http://jsfiddle.net/vKr8a/

这是一个演示:http: //jsfiddle.net/vKr8a/

See this compatibility table for more info:

有关详细信息,请参阅此兼容性表:

http://kangax.github.com/es5-compat-table/

http://kangax.github.com/es5-compat-table/

回答by digout

Alternatively, you could use a recommended polyfill for browsers that don't natively support Object.keys

或者,您可以为本机不支持的浏览器使用推荐的 polyfill Object.keys

Object.keys=Object.keys||function(o,k,r){r=[];for(k in o)r.hasOwnProperty.call(o,k)&&r.push(k);return r}

A break down of what this script does:

分解此脚本的作用:

Object.keys = Object.keys || function(o,k,r) { 
// If the script doesn't detect native Object.keys 
// support, it will put a function in its place (polyfill)

    r=[];
    // Initiate the return value, empty array

    for(k in o) r.hasOwnProperty.call(o,k) 
    // loop through all items in the object and verify each
    // key is a property of the object (`for in` will return non 
    // properties)

    && r.push(k);
    // if it is a property, save to return array

    return r
}

回答by nozzleman

Object.keyshas been introduced in ECMAScript 5th Edition. So if you IE-version is lower than 9, it will not be supported.

Object.keys已在 ECMAScript 第 5 版中引入。因此,如果您的 IE 版本低于 9,则将不支持。