Found a few unfamiliar things in a script I'm working with.. not sure what to google!

Hi guys I’m working with a fairly basic function that grabs Url parameters and throws them into an object. I’m seeing a couple of things I haven’t before and I was hoping you guys could help me understand what it means and why it’s being used. I was unsure what to google. Here is the first one in the code I’m unsure of in red:

jQuery.extend({
  getUrlVars: function(){
    var vars = [][COLOR="#FF0000"], hash[/COLOR];
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return jQuery.getUrlVars()[name];
  }
});

In this instance, hash is just another variable. {one clue is the way it is used later in the script}
That ONE line could have been broken into two, like this


var vars = [];
var hash;

That is a javascript convention (not something peculiar to jQuery) that allows the definition of multiple variables on a single line.

D’oh! I swear I need a second set of eyes… completely mistook that for some special javascript sauce I haven’t encountered before… Time for some coffee. And thank you Parkin for pointing out my sheer stupidity :stuck_out_tongue_winking_eye: You’re so mean!

Don’t be so hard on yourself. Personally, I prefer the style where all variables are declared on separate lines. And that is contrary to my nature (where I am a big fan of ternary operators!) to favor concise code over ‘self-documenting’ code.
Enjoy that coffee. I need to go get one too!!

Whereas for me, using multiple var statements in the same area leads you to thinking that multiple var statements are okay, which then leads you down to placing some of those multiple var statements in different locations, which is bad.

So to prevent the risk of such problems the policy of just one var statement at the start of the function guarantees that no such temptation occurs.

It’s like the opening brace style. One style results in syntax errors sometimes occurring, whereas another style guarantees that such problems can never occur, which is why the latter is preferred.

Thanks guys :] I agree with you. I can see where this coder might have been in a bit of a hurry… the project’s deadline is insane. Arguably that would save time… just say’n - under pressure **** happens - unfortunately! Such as me forgetting that you can declare multiple var’s in a row! D’oh! On the bright side there are people such as yourself that are willing to give guys like us a nice little slap in the face in the morning… what was that saying… “the best part of waking up…” LOL

My preferred style is to define all the variables in the second statement within the function but without assigning values - and then assign values in subsequent statements. I feel that makes the declaration easier to read. So instead of:

getUrlVars: function(){
     var vars = [][COLOR=#FF0000], hash[/COLOR]; 
    var hashes ...

I’d have written:

getUrlVars: function(){
    "use strict";
     var vars[COLOR=#FF0000], hash[/COLOR], hashes;
    vars = [];
    hashes ...

By comparison, my preferred style for the var statement is to organise them thematically using a given/when/then grouping. So you start with information that is needed such as hashes, followed by variables that are used by the function (such as i and hash), and end with variables that are used for the final result.


getUrlVars: function () {
     var hashes = ...,
        i,
        hash,
        vars = [];
    for (i = 0; i < ...) {