View Full Version : jQuery Creating objects within .each()

10-07-2010, 10:44 AM

I have a tab compopenet object that works by passing a dom element to a constructor... which then finds all the relative elements to that element using .find() and adds the necessary behaviour.

Now, since I have multiple tab components on the same page, I'm using jQuery each() to iterate through the dom elements and pass each into the tab component constructor function.

Looks something like this:

function TabComponent(el)

new TabComponent($(this));

This works fine for my purposes, but after running my code through JSLint and researching here (http://stackoverflow.com/questions/2381253), I've been made aware that I'm pretty much throwing these objects away, since I'm not assigning them to a variable.

My question really is; is this particularly bad practice under the circumstances? If I assigned the object to a variable within .each(), I'd surely be overwriting that varibale with each iteration and essentially doing the same thing? I'm wondering how a more experienced coder would handle this?

Any help greatly appreciated.