简体   繁体   English

为什么'for循环中没有'undefined`出现?

[英]Why doesn't `undefined` appear in the for…in loop?

I'm a little confused regarding for - in loops: 我就有点糊涂for - in循环:

window.hasOwnProperty(undefined) // Returns true

and

undefined in window // Returns true

but the following does not print Undefined is in window! 但下面打印Undefined is in window!

for(a in window) {
     if(a === 'undefined')
         console.log('Undefined is in window!');
}

Why doesn't undefined appear in the for - in loop? 为什么未在for - in循环中出现undefined

It's not an enumerable property. 这不是一个可枚举的财产。 In modern JavaScript engines, you have control over that in your own objects, but it's pretty much always been the case (well maybe not in every interpreter) that "native" objects could "hide" properties from for ... in iteration. 在现代JavaScript引擎中,您可以在自己的对象中控制它,但for ... in迭代”中,“本机”对象可以“隐藏”属性的情况几乎总是如此(可能并非在每个解释器中)。

You can read about Object.defineProperty() at MDN . 您可以在MDN上阅读有关Object.defineProperty()的信息。 I'm not sure whether there's going to be a syntax (in Harmony) for marking properties as non-enumerable in object literals. 我不确定是否会有一种语法(在Harmony中)将属性标记为对象文字中的不可枚举。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM