Skip to main content

Bug Tracker

Side navigation

#11278 closed enhancement (invalid)

Opened February 03, 2012 07:07PM UTC

Closed March 10, 2012 04:29AM UTC

Last modified September 27, 2012 07:53AM UTC

$.each: no usable message/notice/exception for undefined first argument

Reported by: Owned by:
Priority: low Milestone: None
Component: core Version: 1.7.1
Keywords: needsreview Cc:
Blocked by: Blocking:

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

Attachments (0)
Change History (8)

Changed February 03, 2012 07:12PM UTC by timmywil comment:1

component: unfiledcore
keywords: → needsreview
priority: undecidedlow
status: newopen
type: bugenhancement

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

Changed February 03, 2012 07:58PM UTC by gibson042 comment:2

Changed February 03, 2012 08:01PM UTC by gibson042 comment:3

_comment0: I like this; it makes unnecessary the `jQuery.each( arr || undefined, ... )` idiom and costs almost nothing.1328299325497088

I like this; it makes unnecessary the jQuery.each( arr || [], ... ) idiom and costs almost nothing.

Changed February 03, 2012 08:07PM UTC by dmethvin comment:4

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.

Changed February 03, 2012 11:16PM UTC by anonymous comment:5

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;

however, i'll use this variant : "arr || []" for until invent something more reasonable

Changed March 10, 2012 04:29AM UTC by dmethvin comment:6

resolution: → invalid
status: openclosed

Since this has been consistent behavior of $.each() through several versions, I think we should leave this in the realm of invalid inputs.

Changed September 26, 2012 05:15PM UTC by timmywil comment:7

#12617 is a duplicate of this ticket.

Changed September 27, 2012 07:53AM UTC by comment:8

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.