Chrome中的非枚举属性出现在for循环中 [英] Non-enumerable properties appear in for...in loop in Chrome

查看:123
本文介绍了Chrome中的非枚举属性出现在for循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< - code>在循环中的会经历一个对象的所有可枚举属性,即使是原型链中的那些属性。函数 hasOwnProperty 可以过滤那些原型链中的枚举属性。最后,函数 propertyIsEnumerable 可以区分一个对象的可枚举属性。 因此,以下脚本不应该打印任何东西

  for(a in window)
if(window.hasOwnProperty(a )&&!window.propertyIsEnumerable(a))
console.log(a);

然而,在Chrome上面打印了很多属性名称。



为什么 - 循环和 propertyIsEnumerable 对于枚举类型相互矛盾?

解决方案

可悲的事实是JavaScript引擎是不一样的。虽然符合ES5标准的引擎会忠实地尊重其属性的可枚举性,但正如pimvdb和Felix所指出的那样, window 是一个主机对象,并且不符合ES5的规则。

您可以看到更多关于 window 对象实际位于 window.constructor ,它将显示它是从

 函数Window(){[native code]} 

因此,我们没有真正的方法来确定为什么某些属性可枚举(根据Chrome),并且其他人不在JavaScript运行时。



但是,您仍然可以看到Chrome的不一致窗口的完整状态通过查看 Object.keys(window)来查找可枚举的属性,它依据它的MDN文章,返回所有自己的enu数组



这样做仍会返回一个包含30多个属性的数组。 Chrome浏览器奇怪!



编辑:随着一些进一步的测试,我发现正确的方式让Chrome对窗口的可枚举属性
$ b $ pre $ Object.keys(window).filter(function(prop){
return window.hasOwnProperty(prop)&&!window.propertyIsEnumerable(prop);
});

看起来在Chrome中,枚举属性的候选列表由 for ... in 不是由 Object.keys 返回的,但实际上更接近 Object.getOwnPropertyNames 它列出了可枚举和不可枚举的属性。但是,即使这样也不一致。属性列表 window.hasOwnProperty(prop)&& !window.propertyIsEnumerable(prop)对于中的出现在423和454上Object.getOwnPropertyNames ,分别在我的测试中。


A for-in loop will go through all enumerable properties of an object, even those in the prototype chain. The function hasOwnProperty can filter out those enumerable properties that are in the prototype chain. Finally, the function propertyIsEnumerable can discriminate the enumerable properties of an object.

Therefore, the following script should not print anything:

for(a in window)
    if(window.hasOwnProperty(a) && !window.propertyIsEnumerable(a))
        console.log(a);

On Chrome, however, the above prints a lot of property names.

Why do the for-in loop and propertyIsEnumerable contradict each other regarding enumerables?

解决方案

The sad fact is that JavaScript engines are not the same. While ES5 conforming engines will dutifully respect the enumerability of their properties, window is a host object, as pointed out by pimvdb and Felix above, and not beholden to the rules of ES5.

You can see more evidence of what window object actually is in window.constructor, which will show it as being constructed from

function Window() { [native code] }

As such, we have no real way of determining why certain properties are enumerable (according to Chrome), and others are not from within the JavaScript runtime.

However, you can still see the full state of Chrome's inconsistent window enumerable properties by looking at Object.keys(window) which according to its MDN article, "returns an array of all own enumerable properties found upon a given object."

Doing so still returns an array of 30-odd properties. Chalk it up to Chrome strangeness!

EDIT: With some further testing, I found the proper way for Chrome to make sense as to window's enumerable properties

Object.keys(window).filter(function(prop) {
  return window.hasOwnProperty(prop) && !window.propertyIsEnumerable(prop);
});

It would seem that in Chrome, the candidate list of properties for enumeration by for...in is NOT what is returned by Object.keys, but actually much closer to Object.getOwnPropertyNames which lists enumerable and non-enumerable properties. However, even that is inconsistent. The list of properties that are window.hasOwnProperty(prop) && !window.propertyIsEnumerable(prop) come out to 423 and 454 for for...in and Object.getOwnPropertyNames, respectively, in my testing.

这篇关于Chrome中的非枚举属性出现在for循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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