Will `for (var a of null) {}` throw an error?

Glen Huang curvedmark at gmail.com
Mon Dec 22 07:36:06 PST 2014


Thanks. Found that discussion, and after thinking about it again, throwing errors on null/undefined actually makes sense, because it’s impossible to sliently loop over null/undefined with regular for loop (if you check length directly) or forEach directly. So throwing an error is actually consistent with how things work in es3/5.

> On Dec 22, 2014, at 9:54 PM, Domenic Denicola <d at domenic.me> wrote:
> 
> for in and for of do completely different things and there is no reason to expect consistency between them.
> 
> This was discussed already; search esdiscuss.org.
> 
> On Dec 22, 2014 8:37 AM, Glen Huang <curvedmark at gmail.com> wrote:
> In that case we have a gotcha.
> 
> Is there any interest to change that behavior? Since es6 isn’t finial yet.
> 
> On Dec 22, 2014, at 9:29 PM, Gary Guo <nbdd0121 at hotmail.com <mailto:nbdd0121 at hotmail.com>> wrote:
> 
> On Mon, 22 Dec 2014 21:06:18 +0800, Glen Huang <curvedmark at gmail.com <mailto:curvedmark at gmail.com>> wrote:
> >Ideally it shouldn’t, because its twin `for (var a in null) {}` won’t.
> >
> >But looking at step 8 in https://people.mozilla.org/~jorendorff/es6-draft.html#sec-runtime-semantics-forin-div-ofexpressionevaluation-abstract-operation <https://people.mozilla.org/~jorendorff/es6-draft.html#sec-runtime-semantics-forin-div-ofexpressionevaluation-abstract-operation>, when passing `null` to `GetIterator()`, it will throw a type error when the result of step 2 `CheckIterable(null)`, which is `undefind`, is called in step 3 in the algorithm for `GetIterator()`.
> >
> >Did I miss something or the current spec does throw an error?
> 
> To me since `IsCallable(undefined)` is `false`, so it throws an `TypeError`. Traceur and V8 will both throw a `TypeError` saying that cannot read property @@iterator from undefined or null, and Firefox throws a `TypeError` saying that null have no property. So it should be an error.
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141222/da405d1c/attachment.html>


More information about the es-discuss mailing list