www.webdeveloper.com
Results 1 to 6 of 6

Thread: Odd issue with function arguments being undefined

  1. #1
    Join Date
    Dec 2003
    Posts
    87

    Odd issue with function arguments being undefined

    Hello everyone,

    I currently have a problem with some Javascript code that I can't seem to figure out.
    Full disclosure: I've never really played with Javascript before. I've used (X)HTML, CSS and PHP, but that's about it. I was never really a fan of it, but I was recently tasked with creating a site for someone and we included Google Maps in it.
    Thus, there wasn't really any better way of learning than to dive right in, head-first.

    Anyway, after perusing the Google Maps API documentation and combining the JS syntax and logic with what I remember from PHP, I have the map itself working properly.

    The problem I am having right now is that I am trying to create a function to automatically populate an InfoWindow.
    Specifically, the problem seems to be that if I pass numbers to the function, it works fine, but when I attempt to pass letters to the function, it breaks and my tools are telling me that the argument is undefined. I'm trying to look around for an answer, but I am far too new at this to figure out Javascript's intricacies just yet.

    My function is as follows:
    PHP Code:
    //Function to grab icons for lines requested
    function mtaSmall() {
        var 
    iconArray = Array.prototype.slice.call(arguments);
        var 
    addIcons "";
        for (var 
    0iconArray.lengthi++) {
        
    addIcons addIcons "<img src = \"/res/img/icons/mta/s/" iconArray[i] + ".gif\" alt = \"" iconArray[i] + "\"></img>";
        }
        return 
    addIcons;
        }

    //Requests icons for Lines 1, 2, and 3
    //Works
    var trainInfo = new google.maps.InfoWindow({
        
    contentmtaSmall(123)
        });

    //Requests icons for Lines A, B, and C
    //Does not work - "A is not defined"
    var trainInfo = new google.maps.InfoWindow({
        
    contentmtaSmall(ABC)
        }); 
    Any ideas?

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    It looks as though your parameters are used to form filenames. Either way they must be quoted or they will be treated as variable names:

    Code:
    mtaSmall("A", "B", "C")
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Dec 2003
    Posts
    87
    Quote Originally Posted by Logic Ali View Post
    It looks as though your parameters are used to form filenames. Either way they must be quoted or they will be treated as variable names:

    Code:
    mtaSmall("A", "B", "C")
    Wow, that was exactly it. Thank you!
    For some reason I didn't think of encapsulating them inside quotes like that, since none of the other functions seemed to need them. I also thought the quotes might have gone together with the letters, and the function would wind up trying to search for an "A".gif, or something.

    I am curious, though, why did it work with numbers, but not letters? And even then, it was single letters; I have other functions that are written in very similar formats and they accept unquoted short strings/words without a problem. Is there something special with using singular letters?

  4. #4
    Join Date
    Jun 2008
    Posts
    106
    Quote Originally Posted by DarkScythe View Post
    I am curious, though, why did it work with numbers, but not letters? And even then, it was single letters; I have other functions that are written in very similar formats and they accept unquoted short strings/words without a problem. Is there something special with using singular letters?
    Nah. When you don't use quotes (aka. pass a string), you attempt to pass a reference of the variable instead, not a string.
    The number works because it's simply that, a number. It's not a variable; It's a primitive value. I'm not good at explaining, so maybe someone else can clarify.

  5. #5
    Join Date
    Dec 2003
    Posts
    87
    Thanks!

    My friend also explained it to me, so I understand the reasoning now. It was pretty simple, as he asked what I would have expected to happen if instead of passing mtaSmall(A, B, C) that I had passed mtaSmall(A, B, iconArray) instead. There I was able to say immediately that iconArray did not yet exist as a global array, which led me to realize that I was automatically treating that third parameter as a variable instead of a literal string I was trying to pass A and B as.
    Looking back at my other functions, it's become pretty clear what was happening - even though all the other functions were passed arguments without quotes, they had already been defined as variables that I declared globally.
    I am now much clearer on the usage of quotes to pass either variables or literal strings into my functions.

    The only lingering question I have is whether there is a difference between single quotes and double quotes.

  6. #6
    Join Date
    Jun 2008
    Posts
    106
    Quote Originally Posted by DarkScythe View Post
    The only lingering question I have is whether there is a difference between single quotes and double quotes.
    Nope, no difference at all Cheers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles