I’m a little confused regarding for–in loops:
window.hasOwnProperty(undefined) // Returns true
and
undefined in window // Returns true
but the following does not print 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?
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 ... initeration.You can read about
Object.defineProperty()at MDN. I’m not sure whether there’s going to be a syntax (in Harmony) for marking properties as non-enumerable in object literals.