Ticket #11278 (closed enhancement: invalid)
$.each: no usable message/notice/exception for undefined first argument
| Reported by: | demo_s@… | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | None |
| Component: | core | Version: | 1.7.1 |
| Keywords: | needsreview | Cc: | |
| Blocking: | Blocked by: |
Description
i'm using $.each to iterate through array, got from ajax. if this array variable is undefined, the whole script stops (with error in line 630 of jquery.js 1.7.1)
i spent a lot of time trying to find out the reason. of course, i'll add check for "undefined" of my variables. but i think it should be some check inside 'each' function so this error won't stop the whole script execution or may be it should produce some warnings.
cause whole script on page stops after some ajax fail
Change History
comment:1 Changed 16 months ago by timmywil
- Keywords needsreview added
- Priority changed from undecided to low
- Status changed from new to open
- Component changed from unfiled to core
- Type changed from bug to enhancement
comment:3 Changed 16 months ago by gibson042
I like this; it makes unnecessary the jQuery.each( arr || [], ... ) idiom and costs almost nothing.
comment:4 Changed 16 months ago by dmethvin
The only cost I can see, and it could be a high one, is that we would silently allow undefined to go undetected. In the situation where you actually expect undefined or null, the jQuery.each( arr || [], ... ) documents that clearly.
i spent a lot of time trying to find out the reason
Did the error message in gibson042's test case show up on the console? It seems like it should have. In your program, is undefined a legitimate and anticipated value? If not, it seems like an error message is easier to debug than no message.
comment:5 Changed 16 months ago by anonymous
some of my programs treat undefined as legitimate. others will have no problem if such values will be undefined.
as for me, it'll be more intuitive for $.each to do nothing on 'undefined' instead of generating exception;
| []" for until invent something more reasonable |
comment:6 Changed 15 months ago by dmethvin
- Status changed from open to closed
- Resolution set to invalid
Since this has been consistent behavior of $.each() through several versions, I think we should leave this in the realm of invalid inputs.
comment:8 Changed 8 months ago by glen.pike@…
IMHO, the behaviour of $.each is inconsistent with (my perception of) jQuery's robust nature. Method chaining does not "blow up" when results are undefined, the chain fails silently and gracefully, which is how I expected $.each to perform.
As a lazy programmer, testing every variable to see if it's defined before passing it off to something as simple as iterator is an anathaema to me, so I believe that the request for this change is a valid one from a jQuery "user" point of view.
Just because "it's always been that way" does not mean it's right, but I accept the fact that changing this could break legacy code.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

I'd be alright with changing that. Let's see what others think.