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
).