Thaumant
Thaumant

Reputation: 2151

For-of loop in node --harmony doesn't work with arrays

When I start node v0.11.14 REPL with --harmony option and try for-of loop, I get:

> for (var i of [3, 4, 5]) console.log(i);
TypeError: undefined is not a function

The same for sets. But it works fine with generators:

> function* Counter() { var n=3; while (n < 7) { yield n++; } }
> var c = new Counter();
> for (var i of c) console.log(i);
3
4
5
6

Though the first example from ecmascript wiki page is:

for (word of ["one", "two", "three"]) {
    alert(word);
}

MDN page and Traceur docs contain the same example. I've failed to google "for-of in nodejs". Should it actually work in Node or am I missing something?

Upvotes: 1

Views: 3350

Answers (1)

vkurchatkin
vkurchatkin

Reputation: 13570

for .. of and iterable objects were implemented separately in v8. It looks like in v8 bundled with node 0.11.14 you can use it only with generators.

You can check obj[Symbol.iterator] property to see if object is iterable, it should be a function. In my node build with v8 3.29.93 everything works as expected. So, you probably have to wait for the next 0.11 release (or 0.12).

Upvotes: 1

Related Questions