Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

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

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;

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.

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

http://jsfiddle.net/4Rab7/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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++;
}

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

See this compatibility table for more info:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...