Open
Description
In the end of the chapter there's an example and statement:
var myObject = {
a: 2,
b: 3
};
Object.defineProperty( myObject, Symbol.iterator, {
enumerable: false,
writable: false,
configurable: true,
value: function() {
var o = this;
var idx = 0;
var ks = Object.keys( o );
return {
next: function() {
return {
value: o[ks[idx++]],
done: (idx > ks.length)
};
}
};
}
} );
// iterate `myObject` manually
var it = myObject[Symbol.iterator]();
it.next(); // { value:2, done:false }
it.next(); // { value:3, done:false }
it.next(); // { value:undefined, done:true }
// iterate `myObject` with `for..of`
for (var v of myObject) {
console.log( v );
}
// 2
// 3
Note: We used
Object.defineProperty(..)
to define our custom@@iterator
(mostly so we could make it non-enumerable), but using theSymbol
as a computed property name (covered earlier in this chapter), we could have declared it directly, likevar myObject = { a:2, b:3, [Symbol.iterator]: function(){ /* .. */ } }
.
I'm somewhat confused by the part:
(mostly so we could make it non-enumerable)
MDN states that Symbol.iterator
is non-enumerable, my own experimentation is a little bit confusing...
const iterable = {
a: 1,
[Symbol.iterator]: function() {}
};
for (x in iterable) console.log(x); // a
console.log(iterable.propertyIsEnumerable(Symbol.iterator)); // true ...WTF?!
Is there something else we should know?