Bug Tracker

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#11278 closed enhancement (invalid)

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

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

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 (8)

comment:1 Changed 11 years ago by Timmy Willison

Component: unfiledcore
Keywords: needsreview added
Priority: undecidedlow
Status: newopen
Type: bugenhancement

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

comment:3 Changed 11 years ago by gibson042

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

Last edited 11 years ago by gibson042 (previous) (diff)

comment:4 Changed 11 years 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 11 years 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;

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

comment:6 Changed 11 years ago by dmethvin

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.

comment:7 Changed 11 years ago by Timmy Willison

#12617 is a duplicate of this ticket.

comment:8 Changed 11 years ago by [email protected]

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.

Note: See TracTickets for help on using tickets.