Iamsamstimpson
Iamsamstimpson

Reputation: 1357

jQuery Optimisation Multiple Selectors

At the top of my script I two variables holding two selectors:

    var all_open = $('ul#festival_dates li.controls a:eq(0)');
    var all_close = $('ul#festival_dates li.controls a:eq(1)');

I'm getting there with Jquery, I'm now at the point where I'm looking at my code and finding ways to write much more efficient scripts. One thing I noticed using YUI Compressor is that it said "[WARNING] Try to use a single 'var' statement per scope." and highlighted the two lines above, has anyone got any suggestions about this, or can explain a more technical description of what this means?

Any help would be much appreciated.

Thank you for your replie.

Upvotes: 3

Views: 631

Answers (2)

David Thomas
David Thomas

Reputation: 253318

Instead of repeating the word var you can use commas to delimit additional variable declarations:

var all_open = $('ul#festival_dates li.controls a:eq(0)'),
all_close = $('ul#festival_dates li.controls a:eq(1)');

Upvotes: 2

Joe
Joe

Reputation: 82604

What it' saying is this:

var all_open = $('ul#festival_dates li.controls a:eq(0)'),
    all_close = $('ul#festival_dates li.controls a:eq(1)');

to use the comma operator.

As for performance, http://jsperf.com/single-var, I'm not sure it makes much of a difference.

Upvotes: 8

Related Questions