I’m working on a few projects with jQuery that need event handling. Since I like the way event handling is done in jQuery, I decided to use it to add event handling to internal objects with jQuery (1.2.6 at the time of writing). This seems very easy:
internalObject = {/* code here */};
eventHandler = $(internalObject);
This means that you could now do
eventHandler.bind('customEvent', function (ev) {
// this is a reference to internalObject
});
and of course also
eventHandler.trigger('customEvent', {some: 'data'});
which is great.
But in a part of my code this just wouldn’t work. Unfortunately it was my first try at doing jQuery({/* custom object */}) and since I obviously wasn’t at my best when writing it I was sure this couldn’t be done. So I hacked around it.
A few weeks ago I made me a test case and figured out it works when passed an empty object ({}). So I implemented it everywhere and sure enough a part of the code stopped working. Which made me delve into the code to figure out why - a bit of debugging made me think the bug was somewhere in jQuery and I found out that there is a problem with the makeArray function. When it’s trying to create an Array it will test if the object has a length property. But as the comment in uncompressed code neatly points out //the window, strings and functions also have ‘length’ it checks a few things that allows it to handle these objects differently.
This means that
eventHandler = $({});
will work normally, while
eventHandler = $({length:0});
won’t because eventHandler length will be 0.
My problem was that the object I was passing had a length property. As such it got sucked into the else that processes arrays and other array like objects (like arguments and most probably also NodeLists). This meant that what I got back was nothing since length was initially set at 0.
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=0f93ccf0-a352-4088-a3b2-1b7a473012ad)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=d4b470d8-c0d7-42f1-b998-9d385fbcc61b)

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=7504f2ed-9fcd-43bb-88ec-2db288896ad1)
