for-of loops, IteratorClose and the rest of the iterations in the spec
André Bargull
andre.bargull at udo.edu
Wed Sep 10 13:28:00 PDT 2014
> >>>/ Seems to me like it's just a matter of wrapping the relevant bits (which ones, though?
> />>/ whatever constitutes the loop "body"
> />/
> />/ OK. That's fairly straightforward.
> />/
> />>/ You also will need a finally clause, if the loop body contains any explicit returns.
> />/
> />/ It only does when the iterator claims there are no more things to be had, in which case do I still want to be calling the return() thing?
> /
> No, you only call 'return' when exiting the loop before the iterator has indicated that it is 'done'..
>
Just for the sake of completeness, here is complete translation of
`for-of` to ES5 syntax.
ES6 for-of statement:
---
for (var value of iterable) {
if (doReturn) return;
if (doBreak) break;
if (doContinue) continue;
otherExpression;
}
---
Translated to ES5 syntax:
---
var $iterable = iterable;
if ($iterable === undefined || $iterable === null) {
throw new TypeError();
}
var $iterator = Object($iterable)[Symbol.iterator]();
if (Object($iterator) !== $iterator) {
throw new TypeError();
}
while (true) {
var $nextResult = $iterator.next();
if (Object($nextResult) !== $nextResult) {
throw new TypeError();
}
if ($nextResult.done) {
break;
}
var $nextValue = $nextResult.value;
var $callClose = false;
try {
value = $nextValue;
if (doReturn) { $callClose = true; return; }
if (doBreak) { $callClose = true; break; }
if (doContinue) continue;
otherExpression;
} catch ($exception) {
try {
if ("return" in $iterator) {
$iterator.return();
}
} catch ($ignore) {
// ignore
}
throw $exception;
} finally {
if ($callClose) {
if ("return" in $iterator) {
$iterator.return();
}
}
}
}
---
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140910/0799da0f/attachment-0001.html>
More information about the es-discuss
mailing list