Destructuring assignment in generator expressions and array comprehensions

Posted by Eli Grey on Stack Overflow See other posts from Stack Overflow or by Eli Grey
Published on 2010-03-25T23:36:12Z Indexed on 2010/03/26 0:33 UTC
Read the original article Hit count: 359

Filed under:

Why does for ([] in object); work fine but [void 0 for ([] in object)] or (void 0 for ([] in object)) throw a syntax error for invalid left-hand assignment?

For example, I would expect the following code to work, but it doesn't (the assertion isn't even done due to the syntax error):

let (
  i = 0,
  arr = [1, 2, 3, 4],
  gen = (i for (i in arr) if (arr.hasOwnProperty(i))
) {
  for ([] in gen) i++;
  console.assertEquals([void 0 for ([] in gen)].length, i);
}

© Stack Overflow or respective owner

Related posts about JavaScript