Bug Tracker

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#11167 closed bug (wontfix)

Unable to build a jquery collection from objects which have a .length property.

Reported by: [email protected] Owned by: [email protected]
Priority: low Milestone: None
Component: unfiled Version: 1.6.4
Keywords: Cc:
Blocked by: Blocking:


Adding an object which has a length property into a jQuery 'collection' doesn't produce the expected result. I've been unable to find documentation of this behaviour.

In code. The following happens in Firefox 9 and Chromium 14 under ubuntu.

var one = 1;
console.log( $(one), $(one).length ); // jQuery(1), 1

var arrayTwo = [2];
console.log( $(arrayTwo), $(arrayTwo).length ); // jquery([2], 1

var o = new Object;
console.log( $(o), $(o).length ); // jquery( Object {} ), 1

var EmptyFunction = function(){};
var emptyFunctionObject = new EmptyFunction();
console.log( $(emptyFunctionObject), $(emptyFunctionObject).length ); // jQuery( Object {} ), 1

// this is surprising
var Length = function( length ){ this.length = length };
var l0 = new Length(0);
var l1 = new Length(1);
var l5 = new Length(5);
console.log( $(l0), $(l0).length ); // jQuery(), 0
console.log( $(l1), $(l1).length ); // jQuery(undefined), 1
console.log( $(l5), $(l5).length ); // jQuery(undefined), undefined, undefined, undefined, undefined), 5

// same with
var oLength  = {length:2};
console.log( $(oLength), $(oLength).length ); // jQuery(undefined, undefined), 2

It seems you can't make a collection from a object with a length property. In any case the collection returned does not behave as expected.

Kind Regards, Pete

Change History (3)

comment:1 Changed 11 years ago by dmethvin

Owner: set to [email protected]
Status: newpending

jQuery is primarily meant for manipulating collections of DOM objects. What is your application for wrapping plain objects in jQuery? We do support them in limited scenarios but only a few methods apply.

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

Status: pendingnew


Thanks for the response. I ran into this when using custom events on objects. For sometime now I've been implementing a publish / subscribe system with bind() and trigger().

The workaround (not use a object with a length property) hasn't caused me any bother (after I figured it out) it was just unexpected, initially confusing and well kind of arbitrary edge case to a otherwise (seemingly) otherwise robust system.

Regards, Pete

comment:3 Changed 11 years ago by addyosmani

Priority: undecidedlow
Resolution: wontfix
Status: newclosed

Thanks for getting back to us.

As Dave has already mentioned, we only support a few methods when dealing with plain objects wrapped in jQuery and as you said, it's not very difficult to work around this limitation for your use case. With this in mind, I don't think there's enough reason for us to change this behaviour in core. I would go as far as saying that if your application is relying heavily on using pub/sub against objects, opt for a simple vanilla JS pub/sub system over .bind()/.trigger() - it's not that much more code :)

Note: See TracTickets for help on using tickets.