Object.keys()是否可以在Internet Explorer 9中用于内置对象?

| Object.keys()方法对我来说适用于以下代码:
var foo = {foo: 1, bar: 2};
console.log(Object.keys(foo).length);
但是,Object.keys()为内置对象返回一个零长度数组,其代码如下:
<!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。 后记:我仍然不清楚为什么这样做(例如):
    for (prop in performance.timing) {
        if (performance.timing.hasOwnProperty(prop)) {
            console.log(prop); 
        }
    }
...在IE9中什么也没产生,但是效果很好:
for (prop in performance.timing) {
    console.log(prop); 
}
    
已邀请:
        在JavaScript中,存在本机对象和宿主对象。通常,您可以依靠诸如ѭ4之类的东西来处理本机对象,而不能使用宿主对象。
window
document
等是宿主对象。 IE尤其以其宿主对象不是本机对象而闻名(宿主函数不具有
call
apply
功能,等等)。 当然,也可以是
document
没有不可枚举的性质。对象的大多数默认属性都是不可枚举的,因此不会出现在“ 4”中。例如,
Object.keys([]).length
Object.keys(new RegExp(\".*\")).length
都是ѭ13neither,因为即使它们都有很多属性,它们都不具有任何可枚举的属性(它们都具有其所有“方法”的属性,当然空白数组具有
length
属性,而
RegExp
拥有
lastIndex
属性)。 更新:实际上,这是难以计数的。试试这个测试:
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
默认情况下没有任何可枚举的属性。 (相比之下,在Chrome上,我首先获得65个可枚举的属性,当我添加自己的expando时,当然有了66个。) 这是一个比较完整的测试页(实时副本)(很快就被黑了,不是一件美事):
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;
  }

};
    

要回复问题请先登录注册