3

When I run the following Javascript scripts

ite=Iterator(["aaa","bbb"])
ite.next()

I always got an error message as follows.

enter image description here

But it is my first next() after creating it, why the iterator stopped working? In addition, why can we omit the "new" when we create an object in Javascript? I use Javascript in MongoDB v2.2.4 client.

13
  • 5
    In what library did you find this Iterator ? This isn't standard JavaScript. Commented May 23, 2013 at 16:44
  • 3
    @dystroy: Wrong. developer.mozilla.org/en-US/docs/JavaScript/Guide/… Commented May 23, 2013 at 16:46
  • 1
    @SLaks: Kinda not wrong. It's not standard. stackoverflow.com/questions/12079417/javascript-iterators Commented May 23, 2013 at 16:49
  • 2
    @user2384994: To be clear, when most people refer to "JavaScript", technicalities aside, they refer to the fairly-standardized subset that exists in most modern browsers. The V8 JavaScript engine has no Iterator. Commented May 23, 2013 at 16:54
  • 1
    Hmm this might be a bug but I don't think it is, I see the documentation specifically passes the param by reference, what if you do the same? Commented May 23, 2013 at 16:54

1 Answer 1

3

Basically, the Iterator that you've learned about doesn't exist in all JavaScript engines - or, more correctly, in all ECMAScript engines (JavaScript is technically the Mozilla variant of ECMAScript).

There's documentation on MDN because (surprise!) MDN most often covers the version of JavaScript used in Firefox.

The current version of MongoDB, on the other hand, uses the V8 JavaScript engine (the same one that is in Chrome), so it does not have Iterator.


Unfortunately, this does not explain why you didn't get a ReferenceError. The version of MongoDB that you are using most likely contained an engine that has Iterator available.

In testing this behavior in Firebug, I get even weirder results.

>>> ite = Iterator(['aaa','bbb']);
>>> ite.next();
// Nothing
>>>
>>> var ite = Iterator(['aaa','bbb']);
>>> ite.next();
[0, "aaa"]
>>> ite.next();
[1, "bbb"]
>>> ite.next():
// Nothing

I suspect that Firebug may be suppressing StopIteration, but I have no idea why making the Iterator a global causes it to not iterate. If someone has any insight on this, I'd be interested in hearing it.

In the meantime, you may want to try creating the Iterator as a local variable, as that was the only way I was able to get the iteration to work.

// Notice the 'var'
var ite = Iterator(["aaa","bbb"]);

Yes, as pointed out by @MikeSamuel in the comments, it appears that when the REPL attempts to display the Iterator, it actually runs it to exhaustion. This would explain why ite = Iterator(...) doesn't work (because it returns the iterator as the result of the expression, which then gets exhausted by the REPL) and why var ite = Iterator(...) works (because the result of a var declaration is undefined).

Sign up to request clarification or add additional context in comments.

14 Comments

it has only used V8 by default in its latest build, before IT did use spidermonkey
Then why doesn't the call to Iterator(...) throw a ReferenceError? It not only succeeds, but the call to next() then throws a StopIteration, which is part of the iteration API.
The [object StopItation] is a suspiciously specific error if the engine doesn't support iterators.
Infact the docs even mention the error: If a yield is not encountered during the processing of the thrown exception, then the exception will propagate up through the call to throw(), and subsequent calls to next() will result in StopIteration being thrown.
My guess is that when the iterator is created thus ite = Iterator(...) then the REPL tries to display its values, so exhausts it. When you do var ite = ... the result the REPL gets is void, so doesn't do anything with the iterator.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.