Opened 12 years ago
Closed 12 years ago
#6885 closed feature (invalid)
$.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.
Change History (4)
comment:1 Changed 12 years ago by
comment:2 Changed 12 years ago by
Priority: | → undecided |
---|---|
Resolution: | → invalid |
Status: | new → closed |
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.
comment:3 Changed 12 years ago by
Resolution: | invalid |
---|---|
Status: | closed → reopened |
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.
comment:4 Changed 12 years ago by
Resolution: | → invalid |
---|---|
Status: | reopened → closed |
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.
Exactly what are you passing as arrayObject? What would you want jQuery to do in those cases?