Skip to main content

Bug Tracker

Side navigation

#6885 closed feature (invalid)

Opened August 08, 2010 03:43PM UTC

Closed October 10, 2010 07:01PM UTC

$.each to throw error when iterating object is invalid

Reported by: andras Owned by:
Priority: undecided Milestone: 1.4.3
Component: core Version: 1.4.2
Keywords: Cc:
Blocked by: Blocking:
Description

In many cases we write $.each(arrayObject, function(){ ... });

When we give an invalid (undefined) arrayObject is pretty hard to figure out why this routine fails. This enhancement would provide help to beginners using utilities.

Attachments (0)
Change History (4)

Changed August 09, 2010 01:26AM UTC by dmethvin comment:1

Exactly what are you passing as arrayObject? What would you want jQuery to do in those cases?

Changed October 04, 2010 06:45AM UTC by addyosmani comment:2

priority: → undecided
resolution: → invalid
status: newclosed

In my opinion, it would be good practice for beginners to manually vet the arrays or data-structures they are using in their code to ensure they are valid prior to iterating over them using $.each.

That said, I believe that defining a clean output for the suggested enhancement could be a useful step for this enhancement request - for example, what should jQuery output? an alert telling the user an invalid array has been passed? handlers where users can determine whether the array has been accepted or thrown?.

As dmethvin mentioned two months ago, it would be useful to hear back regarding what the desired output of this would ultimately be. If you would like to bring this enhancement up again, please feel free to submit a new ticket referring to this existing one so that we can cross-reference further feedback you may have.

Changed October 10, 2010 03:50PM UTC by andras comment:3

resolution: invalid
status: closedreopened

Hi, sorry, i've been busy.

$.each(undefined, function(){ ... }); are more common than thought: Iterate over an empty or yet undeclared property:

$.each(object.prop, function(){ ... }); I was wondering if it could just be added

throw "Attempting to iterate over undefined"

In my copy this is what i have round the 550 line:

var ... length = object.length, ... ;

undefined.length is probably causing this issue.

Changed October 10, 2010 07:01PM UTC by dmethvin comment:4

resolution: → invalid
status: reopenedclosed
The $.each() function can be used to iterate over any collection, whether it is a map (JavaScript object) or an array. -- http://api.jquery.com/jQuery.each/

There are many invalid inputs that someone might pass to

jQuery.each
, such as
undefined, NaN, null, Date
, or most DOM objects. The caller is responsible for passing sane input. It would be too expensive to check for these and throw errors. If you are getting an error on
undefined.length
then simply use a debugger like Firebug and see what code is calling jQuery with bad input.