与 IE8 兼容的 JavaScript Object.keys() 方法替代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20705063/
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-27 19:03:38  来源:igfitidea点击:

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

javascriptinternet-explorer-8

提问by Mikarma

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

我在 IE8 中测试不支持 Object 方法时出错。我正在使用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?

对于 IE8 支持的这种方法,是否有好的解决方法?

回答by Daab

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

Mozilla 有关于如何在旧浏览器中 polyfill 该功能的说明: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;
    };
  }());
}

回答by thefourtheye

If jsoncontis an object, you can use for...in

如果jsoncont是对象,则可以使用for...in

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

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

或者按照 中的建议this blog post,您可以像这样创建它

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

回答by naorz

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).

以下代码支持(使用 MDN 文档)与所有浏览器(IE6+,请注意,我还没有在 IE 上测试它,仅通过文档)。

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

您可以使用js 兼容性检查器进行检查

What we have here:

我们这里有什么:

  1. for...inAll browsers (IE6+)
  2. hasOwnPropertyAll browsers
  3. Function.prototype.callAll browsers
  4. continueAll browsers
  5. Array.prototype.pushAll browsers (IE 5.5 +)
  1. for...in所有浏览器(IE6+)
  2. hasOwnProperty所有浏览器
  3. Function.prototype.call所有浏览器
  4. continue所有浏览器
  5. Array.prototype.push所有浏览器(IE 5.5 +)

Summary: IE 6+support

总结:IE 6+支持

Note that you can use iteratefunction to iterate objectsand arraysas your wish (IE 6+).

请注意,您可以使用iterate函数来遍历objectsarrays你的愿望(IE 6+)。