Reputation: 6990
Following code
a = {
b () {return 3;},
[Symbol.iterator] () {return 4;}
};
console.log (a ['b']);
console.log (a [Symbol.iterator]);
console.log (111);
for (var attrib in a) {
console.log (attrib);
console.log (a [attrib]);
}
console.log (222);
prints
function b() {return 3;}
function () {return 4;}
111
b
function b() {return 3;}
222
Why is the second function, returning 4 not printed in the for loop. How can I make a for-loop where all attributes are printed, including the special ones such as [Symbol.iterator]?
N.B. The question is not how to write a correct iterator function, I know I haven't done that.
Upvotes: 1
Views: 34
Reputation: 1451
Symbol.iterator is a Symbol and you cannot list them like that, you have to use Object.getOwnPropertySymbols(your_object) to list them.
For ... in ... loop lists object's properties indeed but symbols are a bit different kind of beast and they require special treatment.
Upvotes: 2