Side navigation
#11167 closed bug (wontfix)
Opened January 12, 2012 01:03PM UTC
Closed January 30, 2012 11:03AM UTC
Last modified March 14, 2012 10:22AM UTC
Unable to build a jquery collection from objects which have a .length property.
Reported by: | pctuthill@gmail.com | Owned by: | pctuthill@gmail.com |
---|---|---|---|
Priority: | low | Milestone: | None |
Component: | unfiled | Version: | 1.6.4 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
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
Attachments (0)
Change History (3)
Changed January 12, 2012 02:48PM UTC by comment:1
owner: | → pctuthill@gmail.com |
---|---|
status: | new → pending |
Changed January 25, 2012 05:47PM UTC by comment:2
status: | pending → new |
---|
Dmethvin,
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
Changed January 30, 2012 11:03AM UTC by comment:3
priority: | undecided → low |
---|---|
resolution: | → wontfix |
status: | new → closed |
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 :)
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.