How to advance an iterator.

37 views
Skip to first unread message

Jane Chen

unread,
Nov 3, 2015, 6:17:02 PM11/3/15
to v8-users
With the change of behavior in Iterator discussed here:

https://groups.google.com/forum/#!topic/v8-users/8LkrofVr0aA

If I have an iterator (natively defined) which supports for ... of loop, what is the equivalent of calling next to advance it?

I hope the following js example demonstrates what I mean:

> myIterable[Symbol.iterator] = function* () {yield 1;}
function* () {yield 1;}
> for (var k of myIterable) print(k);
1
> myIterable[Symbol.iterator];
function* () {yield 1;}
> myIterable[Symbol.iterator].next();
(shell):1: TypeError: myIterable[Symbol.iterator].next is not a function
myIterable[Symbol.iterator].next();
                            ^
TypeError: myIterable[Symbol.iterator].next is not a function
    at (shell):1:29



Ben Noordhuis

unread,
Nov 3, 2015, 6:40:07 PM11/3/15
to v8-u...@googlegroups.com
What version of V8 is that? It works in 4.6.

Jane Chen

unread,
Nov 3, 2015, 8:45:32 PM11/3/15
to v8-users
4.6.88.  Just tried it in v8 shell.  myIterable[Symbol.iterator].next is undefined.

Adam Klein

unread,
Nov 3, 2015, 8:52:59 PM11/3/15
to v8-users
You're missing a function call:

myIterable[Symbol.iterator]().next()  // note parens after the closing bracket

This follows the spec-internal method GetIterator: http://tc39.github.io/ecma262/#sec-getiterator

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jane Chen

unread,
Nov 3, 2015, 9:51:18 PM11/3/15
to v8-users
Ah, you are right.  Thanks!
Reply all
Reply to author
Forward
0 new messages