Bug Tracker

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#5302 closed bug (wontfix)

$.each fails on certain objects containing a property-key "length"

Reported by: Marc Jansen Owned by:
Priority: major Milestone: 1.4
Component: core Version: 1.3.2
Keywords: Cc: Marc, Jansen
Blocked by: Blocking:

Description

when $.each is called with an object, that has an attribute named length, it does not iterate over the object:

var humpty = {
  'length' : 'foobar'
};
$.each(humpty, function (i, n) {
  alert('$.each: ' + i);
});

Using the traditional for-in-way it works:

for( objKey in humpty ) {
  alert('for in: ' + objKey);
}

In the function it is only tested whether length is not equal to undefined.

The function should check the nature of the object to iterate over differently:

perhaps: iterObj.constructor === Object iterObj.constructor === Array

Change History (3)

comment:1 Changed 10 years ago by Marc Jansen

Sorry, the last line should read

// perhaps: 
iterObj.constructor === Object 
iterObj.constructor === Array 

comment:2 Changed 10 years ago by flesler

Cc: Marc Jansen added
Component: unfilledcore
Resolution: wontfix
Status: newclosed

This is the desired behavior. You can always write your iterator function that works differently.

comment:3 Changed 10 years ago by steadicat

I really don't see why this would be desired behavior. Just in case this is reopened some day, this is my patch:

http://github.com/steadicat/jquery/commit/8d7948ad5417695af41d8be9851a4f85021a7b3c

Note: See TracTickets for help on using tickets.