SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    jQuery.extend({
      getUrlVars: function(){
        var vars = [], hash;
        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];
      }
    });

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,285
    Mentioned
    181 Post(s)
    Tagged
    4 Thread(s)
    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
    Code:
    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.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ;P You're so mean!

  4. #4
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,285
    Mentioned
    181 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by sethwb View Post
    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 ;P 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!!
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by ParkinT View Post
    Don't be so hard on yourself. Personally, I prefer the style where all variables are declared on separate lines.
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,603
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    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:

    Code:
    getUrlVars: function(){
         var vars = [], hash; 
        var hashes ...
    I'd have written:

    Code:
    getUrlVars: function(){
        "use strict";
         var vars, hash, hashes;
        vars = [];
        hashes ...
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by felgall View Post
    Code:
    getUrlVars: function(){
        "use strict";
         var vars, hash, 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.

    Code javascript:
    getUrlVars: function () {
         var hashes = ...,
            i,
            hash,
            vars = [];
        for (i = 0; i < ...) {
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •