Object.keys无法在Internet Explorer中工作 [英] Object.keys not working in internet Explorer

查看:350
本文介绍了Object.keys无法在Internet Explorer中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


  • 应用程序初始化

  • 获取JSON

  • 得到JSON

  • 得到钥匙(不在IE中显示)

  • App Initializing
  • Getting JSON
  • Got JSON
  • Got Keys (Does not show in IE)

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

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

我不再确定对象.keys是问题所以这里是整页的链接。
我在页面中使用JavaScript以便于查看

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

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: {}
}


推荐答案

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

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

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

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

    return keys;
  };
}

这是一个更全面的polyfill:

Here is a more comprehensive 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;
    };
  }());
}

这篇关于Object.keys无法在Internet Explorer中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆