> "I think the use of $ before such variables is merely a convention"

It's absolutely nothing more than that, just a convention...

But it makes sense to use the $ on a variable name to signify to you
later on, or more importantly someone else looking/working on your
code at a later point, that "hey, this is a reference to a jQuery
object, so do jQuery stuff to it"

> "But what is the purpose of .get() then?"

Nothing explains it better than the docs (http://docs.jquery.com/Core/
get)

"his serves as a backwards-compatible way of accessing all matched
elements (other than the jQuery object itself, which is, in fact, an
array of elements). It is useful if you need to operate on the DOM
elements themselves instead of using built-in jQuery functions"

So in the example on that page, whoever coded that wants to do a
".reverse()" on the array of DOM objects...

simply saying

$("div.SomeClass").reverse()

won't work because there is no (built in) jQuery method that does that

so by doing ".get()" first, that turns it into am array of DOM objects
instead of jQuery objects and *then* .reverse() would work

var array_of_reversed_dom_objects = $("div.SomeClass").get().reverse
();



On Dec 2, 4:38 pm, hsfrey <hsf...@gmail.com> wrote:
> Thank you Mike.
>
> That makes sense!
>
> But what is the purpose of .get() then? What could I do with an
> "actual JavaScript Array object" which I couldn't do the way you
> demonstrate?
>
> Harvey
>
> On Dec 2, 11:48 am, Michael Geary <m...@mg.to> wrote:
>
> > The jQuery object - the return value from $(whatever) - is an array-like
> > object with .length and [0] etc. properties. You don't need to call .get()
> > on it unless you need an actual JavaScript Array object, which is rare.
>
> > The *elements* of the jQuery object - or the elements of the Array that
> > .get() returns, are plain HTML elements - DOM nodes. They are not jQuery
> > objects themselves. They have DOM methods and properties, not jQuery
> > methods.
>
> > You can always wrap a DOM node in a jQuery object with $(theDomNode).
>
> > You can use a for loop just like the one you have, directly on a jQuery
> > object without calling .get(). Or you can use jQuery's .each() method, which
> > is often cleaner. One way to write your loop would be like this:
>
> >     $('.resizable').each( function( i, box ) {
> >         console.log( box.id + ': left=' + $(box).css('left') );
> >     });
>
> > In this code, 'box' is the same as 'boxes[i]' in your for loop. Note the
> > difference between box.id - referencing a property of the DOM node directly
> > - and $(box).css() - wrapping the DOM node inside another jQuery object and
> > then calling a jQuery method on that object.
>
> > -Mike

Reply via email to