It's mostly done to improve performance. If you save the jQuery object
in a var you only need to find the element once (depending on the
selector it's an expensive operation), and then operate on that single
object. I (and other people) like to put a '$' in front of the var
name when it contains a jQ object, to make it clear:

var $mySet = $('.mystuff'); //jQ object
$mySet.click(...).append(...)

var myElement = $('#someelement')[0]; //HTML element
$(myElement).doSomething();

by doing $($mySet) you're unnecessarily creating a new object from the
one you have already stored. It's not really much overhead, but bloats
code.

ggerri: this.something() where 'something' is a jQuery method is
usually found in plug-ins:

jQuery.fn.doSomething = function(){
     // in here, 'this' refers to the current jQuery object/set
     //so you can use methods on it, like each
     this.each(function(){
        // here, 'this' is the current element in the loop, not a jq
object
        //so you need to wrap it in jQ before using it
        $(this).append(...)
     });
};

cheers,
- ricardo

On Mar 5, 9:58 am, "Rick Faircloth" <r...@whitestonemedia.com> wrote:
> Hi, Rayn :handshake: :o)
>
> I think, for shorthand notation (some say for readability, but I think 
> otherwise),
> some set var's (variables) to represent pieces of code, for instance:
>
> var mySet = '$(mySet)'
>
> and then use it as:
>
> mySet.find(':text')...
>
> Written in "longhand", it would be:
>
> $('mySet').find(':text')...
>
> When trying to read someone else's code, where this shorthand is use 
> extensively,
> I just find it hard to decipher, since I have to trace all the var's down to 
> find
> out what they stand for...
>
> Someone please correct me if I'm wrong...
>
> Rick
>
> -----Original Message-----
> From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On 
> Behalf Of ggerri
> Sent: Thursday, March 05, 2009 7:31 AM
> To: jquery-en@googlegroups.com
> Subject: [jQuery] Re: Simple one: difference between varXy.find(':text') and 
> $(varXy).find(':text')
>
> Thanks Ryan :handshake:
>
> so  mySet.find(':text').each(...) would be right and
> $(mySet).find(':text').each(...) not? :confused:
>
> In examples I often see (within an each function): $(this).something but also 
> this.something
>
> Still dont get the difference of use :,(
>
> G
>
> ryan.joyce...@googlemail.com wrote:
>
> > mySet is an object or variable, $(mySet) will try to get an element
> > using the contents of mySet as the selector.
>
> > On Mar 5, 10:04 am, ggerri <gerald.ressm...@ewz.ch> wrote:
> >> Hi there
>
> >> thats an easy one for you ;-)
>
> >> if i do:
>
> >> var mySet = $('tr>td:nth-child(2n)');
>
> >> how do I use mySet? What's the difference between
>
> >> mySet.find(':text')
>
> >> and
>
> >> $(mySet).find(':text')
>
> >> Thanks :-))
> >> GGerri
>
> >> --
> >> View this message in
> >> context:http://www.nabble.com/Simple-one%3A-difference-between-varXy.find%28%...
> >> Sent from the jQuery General Discussion mailing list archive at
> >> Nabble.com.
>
> --
> View this message in 
> context:http://www.nabble.com/Simple-one%3A-difference-between-varXy.find%28%...
> Sent from the jQuery General Discussion mailing list archive at Nabble.com.

Reply via email to