SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question accessing an undefined variable?

    Hi all,

    I am looking through javascript functions in a .js file for site here -- basically, to learn and understand the javascript written by veteran developers. While I basically understand the script below, I am lost at what the "d" variable stands for since it is undefined using "var" or "d = something". It's like it came out of nowhere, but "a" knows what it is. ?

    How does the script know what the "d" variable is in order to assign it's value to "a" if "d" appears to be (to me) undefined?

    Code:
    function fixSlidingboard(){
    	var a;
    	if(a = d('boardad')){
    		var b = a.getElementsByTagName('embed');
    		for(i=0;i<b.length;i++){
    			if(b[i].getAttribute('height')==1){
    				b[i].setAttribute("height","0");
    			}
    		}
    	}
    }

  2. #2
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ---

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    d looks like a function that will have been defined somewhere else in the script. It has been passed the argument 'boarded' and will return some value based on that argument. The result it returns is then assigned to the variable a which was declared in the line above by the var a; statement.

    Variables can be declared
    Code:
    var a;
    and then assigned a value later
    Code:
    a = d('boarded');
    The code in your example also tests the result of d('boarded') as well as assigning it to the variable a.
    Code:
    if(a = d('boarded'))
    This assigns d('boarded') to a and performs the test if(d('boarded')) in one go. The code inside the if block is only executed if d('boarded') equates to true.

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    d, as you've shown it above, is a function call. So there is probably a function defined somewhere (either in the same javascript file or in one loaded before this one) that has something like:

    function d(someVar) {
    ...
    }


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
  •