Skip to main content

Bug Tracker

Side navigation

#5302 closed bug (wontfix)

Opened September 28, 2009 01:28PM UTC

Closed September 28, 2009 02:50PM UTC

Last modified November 13, 2009 07:22PM UTC

$.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

Attachments (0)
Change History (3)

Changed September 28, 2009 01:31PM UTC by Marc Jansen comment:1

Sorry, the last line should read

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

Changed September 28, 2009 02:50PM UTC by flesler comment:2

cc: → Marc, Jansen
component: unfilledcore
resolution: → wontfix
status: newclosed

This is the desired behavior.

You can always write your iterator function that works differently.

Changed November 13, 2009 07:22PM UTC by steadicat comment:3

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