Object.keys()在Internet Explorer 9中是否适用于内置对象? [英] Does Object.keys() work in Internet Explorer 9 for built-in objects?

查看:128
本文介绍了Object.keys()在Internet Explorer 9中是否适用于内置对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Object.keys()方法对我来说适用于以下代码:

The Object.keys() method works fine for me with code like this:

var foo = {foo: 1, bar: 2};
console.log(Object.keys(foo).length);

但是,Object.keys()对于带有如下代码的内置对象返回零长度数组

However, Object.keys() returns a zero-length array for built-in objects with code like this:

<!doctype html> 
<html>

<head>

<title>Object.keys()</title>

</head>

<body>
<script type="text/javascript">
console.log(Object.keys(window.document).length);
</script>

</body>

</html>

我错过了什么吗?我正在使用Internet Explorer 9.0.8112.16421。

Am I missing something? I'm using Internet Explorer 9.0.8112.16421.

后记:我仍然不清楚为什么这样做(例如):

Postscript: I'm still not clear why this (for example):

    for (prop in performance.timing) {
        if (performance.timing.hasOwnProperty(prop)) {
            console.log(prop); 
        }
    }

...在IE9中不产生任何内容,但这有效罚款:

...produces nothing in IE9, whereas this works fine:

for (prop in performance.timing) {
    console.log(prop); 
}


推荐答案

在JavaScript中, em>本地对象和主机对象。通常,您可以依靠诸如 Object.keys 之类的东西来处理本机对象,而不是宿主对象。 窗口文档等是宿主对象。 IE尤其以其宿主对象不是本机对象而闻名(宿主函数没有调用应用功能等)。

In JavaScript, there are native objects and host objects. In general, you can rely on things like Object.keys working with native objects, but not with host objects. window, document, and others are host objects. IE in particular is well-known for its host objects not being native-like (host functions don't have the call or apply feature, etc.).

当然,可能是文档没有 enumerable 属性。对象的大多数默认属性都是不可枚举的,因此不会显示在 Object.keys 中。例如, Object.keys([])。length Object.keys(new RegExp(。*))。length 都为 0 ,因为它们都没有很多 enumerable 属性,即使它们都有很多属性(它们的所有方法,当然空白数组具有 length 属性,而 RegExp 具有 lastIndex 属性)。

Alternately, of course, it could be that document has no enumerable properties. Most of the default properties of objects are non-enumerable and so don't show up in Object.keys. For instance, Object.keys([]).length and Object.keys(new RegExp(".*")).length are both 0 because neither has any enumerable properties even though they both have lots of properties (they have properties for all of their "methods", and of course the blank array has a length property and the RegExp has a lastIndex property).

更新:实际上,这是难以计数的事情。请尝试以下测试:

Update: And in fact, it was the enumerable thing. Try this test:

alert(Object.keys(window.document).length);
window.document.AAA__expando__property = "foo";
alert(Object.keys(window.document).length);

对我来说,在IE9上,这些警报分别为 0和 1。因此, window.document 支持 Object.keys ,这就是 window.document 没有任何 enumerable 属性。 (相比之下,在Chrome上我有65个可枚举的属性,而当我添加expando时当然有66个。)

For me, on IE9, those alerts are "0" and "1", respectively. So window.document supports Object.keys, it's just that window.document doesn't, by default, have any enumerable properties. (In contrast, on Chrome I get 65 enumerable properties to start with, and of course 66 once I've added my expando.)

这里的测试页相当完整(实时复制)(很快就被黑了,不是一件美事):

Here's a rather more full test page (live copy) (hacked-together quickly, not a thing of beauty):

window.onload = function() {

  document.getElementById('theButton').onclick = function() {

    if (typeof Object.keys !== 'function') {
      display("<code>Object.keys</code> is not a function");
      return;
    }
    showKeys("Before adding", Object.keys(window.document));
    window.document.AAAA__expando__foo = "bar";
    showKeys("After adding", Object.keys(window.document));
  };

  function showKeys(prefix, keys) {
    var p, ul;

    keys.sort();
    prefix =
      "[" + prefix +
      "] Keys on <code>window.document</code> (" +
      keys.length +
      ")";
    if (keys.length !== 0) {
      prefix += " (click to toggle list)";
    }
    p = display(prefix);
    if (keys.length !== 0) {
      ul = document.createElement("ul");
      ul.innerHTML = "<li>" + keys.join("</li><li>") + "</li>";
      ul.style.display = "none";
      document.body.appendChild(ul);
      p.onclick = function() {
        ul.style.display =
          (ul.style.display === "none") ? "" : "none";
      };
    }
  }

  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = msg;
    document.body.appendChild(p);
    return p;
  }

};

这篇关于Object.keys()在Internet Explorer 9中是否适用于内置对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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