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

后端 未结 3 1936
野性不改
野性不改 2020-12-06 02:24

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 =          


        
相关标签:
3条回答
  • 2020-12-06 02:36

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

    0 讨论(0)
  • 2020-12-06 02:42

    Alternatively, you could use a recommended polyfill for browsers that don't natively support 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
    }
    
    0 讨论(0)
  • 2020-12-06 02:45

    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/

    0 讨论(0)
提交回复
热议问题