Use of for-in comprehension blocks¶
in blocks in array comprehensions are a Mozilla-specific language extensions that is no longer supported even by SpiderMonkey, and is unlikely to be included in future ECMAScript standards. This language feature should not be used.
in block can be replaced by a (standards-compliant)
of block iterating over a list of property names obtained, for example, from
In the following contrived example, a
in block is used to iterate over the keys
i of an array and construct an array of strings of the form
"v = a[i]", where
v is the value of
var a = [23,,42]; var desc = [for(i in a) i + " = a[" + i + "]"];
The example can be rewritten to use a
of block iterating over
var a = [23,,42]; var desc = [for(i of Object.keys(a)) i + " = a[" + i + "]"];
Object.keys only includes own properties, not properties inherited from a prototype. If the latter behavior is needed, the array comprehension should be replaced by a
in loop that imperatively populates the result array.
Mozilla Developer Network: Array comprehensions: Differences to the older JS1.7.2/JS1.8 comprehensions.
Common Weakness Enumeration: CWE-758.