Skip to content

this & Object Prototypes - Chapter 3: Symbol.iterator enumerablility #1388

Open
@Beaglefoot

Description

@Beaglefoot

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 the Symbol as a computed property name (covered earlier in this chapter), we could have declared it directly, like var 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?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions