JavaScript Object.keys() method alternative for compatibility with IE8

我怕爱的太早我们不能终老 提交于 2020-02-02 04:16:40

问题


I'm getting in error when testing in IE8 that the Object method is not supported. I'm using Object.keys()

Object.keys(jsoncont).sort(function(a,b){
   return b.localeCompare(a)
 }).forEach(function(key) {
    var val = jsoncont[key];

   /* My code here */
 });
}

Is there a good workaround for this method that is supported by IE8?


回答1:


Mozilla has an explanation of how to polyfill the function in older browsers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
if (!Object.keys) {
  Object.keys = (function () {
    'use strict';
    var hasOwnProperty = Object.prototype.hasOwnProperty,
        hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
        dontEnums = [
          'toString',
          'toLocaleString',
          'valueOf',
          'hasOwnProperty',
          'isPrototypeOf',
          'propertyIsEnumerable',
          'constructor'
        ],
        dontEnumsLength = dontEnums.length;

    return function (obj) {
      if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
        throw new TypeError('Object.keys called on non-object');
      }

      var result = [], prop, i;

      for (prop in obj) {
        if (hasOwnProperty.call(obj, prop)) {
          result.push(prop);
        }
      }

      if (hasDontEnumBug) {
        for (i = 0; i < dontEnumsLength; i++) {
          if (hasOwnProperty.call(obj, dontEnums[i])) {
            result.push(dontEnums[i]);
          }
        }
      }
      return result;
    };
  }());
}



回答2:


If jsoncont is an object, you can use for...in

for (var key in jsoncont) {
...
}

Or as suggested in this blog post, you can create it like this

if (!Object.keys) Object.keys = function(o) {
  if (o !== Object(o))
    throw new TypeError('Object.keys called on a non-object');
  var k=[],p;
  for (p in o) if (Object.prototype.hasOwnProperty.call(o,p)) k.push(p);
  return k;
}



回答3:


The following code is supporting (using MDN docs) with all browsers (IE6+, note that I didn't test it yet on IEs, only by docs).

function getKeys(obj) {
  var keys = [];
  iterate(obj, function (oVal, oKey) { keys.push(oKey) });
  return keys;
}

function iterate(iterable, callback) {
  for (var key in iterable) {
    if (key === 'length' || key === 'prototype' || !Object.prototype.hasOwnProperty.call(iterable, key)) continue;
    callback(iterable[key], key, iterable);
  }
}

You can check it using js compatibility checker

What we have here:

  1. for...in All browsers (IE6+)
  2. hasOwnProperty All browsers
  3. Function.prototype.call All browsers
  4. continue All browsers
  5. Array.prototype.push All browsers (IE 5.5 +)

Summary: IE 6+ support

Note that you can use iterate function to iterate objects and arrays as your wish (IE 6+).



来源:https://stackoverflow.com/questions/20705063/javascript-object-keys-method-alternative-for-compatibility-with-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!