Bug Tracker

Modify

Ticket #11167 (closed bug: wontfix)

Opened 17 months ago

Last modified 15 months ago

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

Reported by: pctuthill@… Owned by: pctuthill@…
Priority: low Milestone: None
Component: unfiled Version: 1.6.4
Keywords: Cc:
Blocking: Blocked by:

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

Change History

comment:1 Changed 17 months ago by dmethvin

  • Owner set to pctuthill@…
  • Status changed from new to pending

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 17 months ago by pctuthill@…

  • Status changed from pending to 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

comment:3 Changed 17 months ago by addyosmani

  • Priority changed from undecided to low
  • Status changed from new to closed
  • Resolution set to wontfix

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 :)

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.