Empty Function Paramaters

Discussion in 'JavaScript' started by cesarcesar, Jan 27, 2009.

  1. #1
    How do i declare a default value in a function? I have a simple function

    
    function color_input(id,token){
    
    	if (!empty(token)){
    		document.getElementById(id).style.backgroundColor = '#2A2A2A';
    		document.getElementById(id).style.color = '#DDC58C';
    	}else{
    		document.getElementById(id).style.backgroundColor = '';
    		document.getElementById(id).style.color = '';
    	}
    
    }
    
    Code (markup):
    Called in HTML like

    <input type="text" id="example_name" onblur="color_input('example_name');">
    HTML:
    You will notice that the #2 parameter is missing in the html call. Sometimes i don't have it to send. in PHP i would just set the functions second param like token=''

    function color_input(id,token=''){}
    Code (markup):
    How do i set a default param in a JS function?
     
    cesarcesar, Jan 27, 2009 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi,

    you can either use function color_input(id){ ... and then check for #2 parameter as if(!empty(arguments[1])) ... }
    or you can keep it as you have it - function color_input(id,token){} and when you call this function you always fill the token value - null, or '' if not needed - <input type="text" id="example_name" onblur="color_input('example_name',null);">
     
    lp1051, Jan 28, 2009 IP
  3. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You cannot assign default values to javascript functions. This is very annoying, but the easiest solution is quite simple. Assuming you have a function that has two parameters, but you call it with just one (just like you did in your code), but you want to have a default value for the parameters, you can do something like this:
    
    function test(param1, param2)
    {
       if(!param1)
          param1 = 'default';
    
       //or
    
       if(param2 == 'undefined')
         param2 = 1;
    }
    
    Code (markup):
    Then, if you call the function with just one parameter - param2 will have the value of 1. If you call it without parameters, param1 will be 'default' and param2 will be 1... If a parameter is not set when you call the function, the variable exists within the function scope, but is 'undefined'.
     
    xlcho, Jan 28, 2009 IP
  4. cesarcesar

    cesarcesar Peon

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    @lp1501 - thanks for your suggestion.

    This will be easier though to add this to my function.
    if (typeof token != 'undefined')
    Code (markup):
     
    cesarcesar, Jan 28, 2009 IP
  5. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yep, this is another way to check if the parameter is not set. Javascript variables and types are veeery weird, thus all three methods work for checking if a parameter is passed or not :D
     
    xlcho, Jan 28, 2009 IP
  6. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #6
    you can do:

    
    function func(arg)
    {
      arg = (arg == undefined ? 'default value' : arg);
    }
    
    Code (markup):
     
    yoavmatchulsky, Jan 28, 2009 IP
  7. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #7
    what i tend to do is use json and merge options... for instance:

    // in mootools, you can use $merge...
    var foobar = function(options) {
        var options = $merge({
            "token": "2",
            "margin": "150px",
            "title": "My title"
        }, options);
         
        console.log(options.title, options.margin); // will output 'new title' and '150px'
        console.log(options); // will output the whole object complete with token's default as well
    };
    
    foobar({
        title: "new title"
    });
    
    // or run as default values only...
    foobar();
    
    PHP:
    the advantages are that you can add as many params as you need to via the function call, you don't need to pre-define them in the function definition or anything like that, no relying on the arguments' order - each option has a name space you can find easily.
     
    dimitar christoff, Jan 29, 2009 IP