javascript Object.keys 在 Internet Explorer 中不起作用

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

Object.keys not working in internet Explorer

javascriptinternet-explorer

提问by Peter Saxton

I have a program to return a list of keys from dictionary. The Code works correctly in Chrome, Opera and Firefox but not Internet Explorer. I have added alert comments to close down where the issue is. Below is the code causing the problem. The alerts are shown in the order

我有一个程序可以从字典中返回一个键列表。该代码可在 Chrome、Opera 和 Firefox 中正常运行,但不能在 Internet Explorer 中正常运行。我添加了警报评论以关闭问题所在。下面是导致问题的代码。警报按顺序显示

  • App Initializing
  • Getting JSON
  • Got JSON
  • Got Keys (Does not show in IE)
  • 应用初始化
  • 获取 JSON
  • 得到 JSON
  • 获得密钥(在 IE 中不显示)

I found a similar Question herebut I believe in this example that this isn't the correct question as I created the dictionary so it is a native object.

我在这里找到了一个类似的问题但我相信在这个例子中这不是正确的问题,因为我创建了字典,所以它是一个本地对象。

I am no longer sure that Object.keys is the problem so here is a link to the full page. I JavaScript is in page to make it easier to view

我不再确定 Object.keys 是问题所在,因此这里是完整页面的链接。我 JavaScript 在页面中,以便于查看

http://www.londonlayout.co.uk/dev/live.htm

http://www.londonlayout.co.uk/dev/live.htm

 var myApp = {
    init: function () {
        var def = $.Deferred();
        alert('App Initializing');
        $.getJSON('data/data.json', function (raw) {
            alert('Getting JSON');
            myApp.data = raw;
            $.each(myApp.data, function (code, details) {
                try {
                    myApp.nameDict[details.name] = code;
                }
                catch (e) {}
            });
            alert('Got JSON');
            myApp.names = Object.keys(myApp.nameDict);
            alert('Got Keys')
            def.resolve();
        });
        return def.promise();
    },
    data: {},
    nameDict: {}
}

回答by jabclab

Object.keysis not avaiable in IE < 9. As a simple workaround you could use:

Object.keys不是在IE <9缴费。作为一个简单的解决方法,您可以使用:

if (!Object.keys) {
  Object.keys = function(obj) {
    var keys = [];

    for (var i in obj) {
      if (obj.hasOwnProperty(i)) {
        keys.push(i);
      }
    }

    return keys;
  };
}

Here is a more comprehensive polyfill:

这是一个更全面的 polyfill:

// 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 dalcam

Alternatively if you have access to lodash you can use keys. e.g.

或者,如果您可以访问 lodash,则可以使用keys。例如

_.keys(yourObj);

_.keys(yourObj);