Should functions be iterated as arrays? #2830
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@jashkenas The current behaviour, as implemented in the internal
isArrayLike
function, reflects "yes", based on the fact that functions have a numericallength
property within safe bounds. However, I think the answer should be "no", for reasons I'll give below, and the current PR could be used to change the behaviour accordingly.My case for adding a check against functions and iterating them object-like, rather than array-like, is as follows:
length
to indicate the number of integer keys. One could argue that this function property should have a different name, such asnumArgs
._
.The way
isArrayLike
arrives at its conclusion has been subject of debate in the past (for example in #1590). Opponents often took the stance that checking for a numericallength
property is wrong altogether, sometimes suggesting radical changes in the way Underscore iteration should work. I would like to emphasize that I'm not in that school of thought. To the contrary; I think checking for a numericallength
is essentially correct. However, I do think that thelength
property of functions has the wrong name for what it represents, which means that it should be disregarded when iterating a function.Edit to add acknowledgement: I found one comment (#1590 (comment)) which suggested the same approach. At the time, it drowned in the heated debate.