Not sure if there is a best practices page on the jquery domain
itself, but there are a lot of blog entries about this stuff. Sorry I
don't have any recommended links though. I just try to remember them
all. :p

Sorry my example was too simple, but it seems like you got the idea.

Like many programming languages, storing something that will be
accessed frequently is good to store/cache in a variable.
For example:
$fields = $("input[type=text][name^=field_][value!=test]");

Calling $("input[type=text][name^=field_][value!=test]") everytime you
want to work with it will hit jQuery harder since it has to walk
through the DOM everytime to look for matching elements.

$fields = $("input[type=text][name^=field_][value!=test]");
$fields.attr('disabled','disabled');
$fields.each(function() { ... });
$fields.val('');
$fields.removeAttr('disabled');

Also, it makes code easier to read. :)

On Jul 2, 9:04 am, expresso <dschin...@gmail.com> wrote:
> Ah!  makes sense.  Because a lot of times I can't distinguish between
> something in plain JavaScript and something that is a jQuery object.
> Good to know.  I'll make sure I do the same once I start really
> getting on board with the jQuery syntax.  It's a VERY good practice on
> variables and so I wonder is there a "Best Practices For jQuery" on
> the jquery site that I'm missing?  that would be awesome if there
> was.  If not, how about someone start one and have it be either
> moderated or have the best practices come straight from the jQuery
> team.  Accept request to put in a new standard and have the team
> approve/disapprove.
>
> this kind of stuff would help people like me tremendously to help me
> ramp up quicker for newbies like me who wonder wtf is going on when
> things like this are in place such as the example question I just
> proposed.
>
> On Jul 2, 1:57 pm, James <james.gp....@gmail.com> wrote:
>
> > Yep, as MorningZ said, it's a good practice to use it to differentiate
> > those that are storing a jQuery object.
>
> > $myDiv = $("#div_1");
> > $myDiv.hide();
>
> > It's easier to tell that you're working with a jQuery object.
> > Otherwise, it's just a regular variable.
>
> > On Jul 2, 8:51 am, MorningZ <morni...@gmail.com> wrote:
>
> > > "obviously I think 2nd is a jQuery variable?  If that's the case why
> > > should I care and where is the docs on that? "
>
> > > It just seems to be common practice by a lot of jQuery people smarter
> > > than us to signify it's a jQuery object stored in that variable, it
> > > has zero to do with jQuery itself per se
>
> > > On Jul 2, 2:29 pm, Michael Lawson <mjlaw...@us.ibm.com> wrote:
>
> > > > Just to addon to what Waseem here has said:
>
> > > > When you are using the default jQuery library, the variable $ is set to 
> > > > the
> > > > value of jQuery, which creates a shortcut for you so that you don't 
> > > > have to
> > > > keep typing jQuery everytime you want to access it, you can just type $.
>
> > > > cheers
>
> > > > Michael Lawson
> > > > Development Lead, Global Solutions, ibm.com
> > > > Phone:  1-276-206-8393
> > > > E-mail:  mjlaw...@us.ibm.com
>
> > > > 'Examine my teachings critically, as a gold assayer would test gold. If 
> > > > you
> > > > find they make sense, conform to your experience, and don't harm 
> > > > yourself
> > > > or others, only then should you accept them.'
>
> > > >   From:       waseem sabjee <waseemsab...@gmail.com>                    
> > > >                                                         
>
> > > >   To:         jquery-en@googlegroups.com                                
> > > >                                                         
>
> > > >   Date:       07/02/2009 02:26 PM                                       
> > > >                                                          
>
> > > >   Subject:    [jQuery] Re: var $varname                                 
> > > >                                                          
>
> > > > its not really a jquery variable
>
> > > > in standard js you can say
> > > > var $myvar = "hello";
> > > > and
> > > > var myvar = "hello";
>
> > > > both will work the same
>
> > > > you can access jquery using either the $ or  JQuery
> > > > like
>
> > > > $("#myid").hide();
> > > > JQuery("#myid").hide();
>
> > > > in a line of the JQuery library code you would see something like
>
> > > > JQuery = window.JQuery = window.$ ...
>
> > > > your could say its like short hand code
>
> > > > instead of saying JQuery("") al the time you can just say $("")
>
> > > > On Thu, Jul 2, 2009 at 8:18 PM, expresso <dschin...@gmail.com> wrote:
>
> > > >   what's the difference between:
>
> > > >   var varname
>
> > > >   var $varname
>
> > > >   obviously I think 2nd is a jQuery variable?  If that's the case why
> > > >   should I care and where is the docs on that?
>
> > > >  graycol.gif
> > > > < 1KViewDownload
>
> > > >  ecblank.gif
> > > > < 1KViewDownload
>
>

Reply via email to