Javascript Functions and optional arguments

71,545

Solution 1

Since all you're doing with everything but url and domid is passing it to the $.get, why not do this?

function process_type(url, domid, args) {
    $.get(url, args, function(data) {
        $(domid).html(data);
    });
}

// call it without type
process_type('myurl', 'domid', {domain:'..', scanid:'...'});
// call it with type
process_type('myurl', 'domid', {type: '..', domain:'..', scanid:'..'});

Solution 2

all parameters in javascript are optional, you can use the parameters array inside of a function to access parameters passed ordinally like so:

function myFunction(option1)
{
   var option2 = arguments[1];
   if(arguments[0] == option1)
      alert("Happy Day, Option1 = " + option1 + ", Option2 = " + option2);
}

myFunction("Hello", "World");

Produces: Happy Day, Option1 = Hello, Option2 = World

Hopefully this illustrates how you can use the arguments array to improve some code.

    function process_type(url, domid, domain, scan_id)
    {
            var myOptions = {
               domain: domain,
               scan_id: scan_id
            };

            if(arguments[4])
                myOptions["type"] = arguments[4];

            $.get(url, myOptions,

            function(data)
            {
                    $(domid).html(data);
            });
    }

Then you can call it with the last parameter being the type as optional, if the parameter is passed it is used if not it is omitted.

Also since the actual parameter is optional in the first place you can also add the name to the end of the function definition and use the same if but instead of arguments[4] you'd do if(type) myOptions["type"] = type;

    function process_type(url, domid, domain, scan_id, type)
    {
            var myOptions = {
               domain: domain,
               scan_id: scan_id
            };

            if(type)
                myOptions["type"] = type;

            $.get(url, myOptions,

            function(data)
            {
                    $(domid).html(data);
            });
    }

This Call would include type

 process_type("xxx", "xxx", "xxx", "xxx", "xxx");

where this call would not

 process_type("xxx", "xxx", "xxx", "xxx");

Solution 3

Some simple ways to do it

// 'b' is optional
// the 'null' value would be the default value

function Example1(a,b){
    b = b || null;

    // Some code here
}

function Example2(a,b){
    if(typeof b == 'undefined') b = null;

    // Some code here
}

function Example3(a,b){
    if(b === undefined) b=null;

    // Some code here
}

function Example4(a,b){
    if(!b) b=null;

    // Some code here
}

For unlimited arguments you can use the array 'arguments', example:

function ExampleArguments(){
    for(var i=0; i<arguments.length; i++){
            // Alert the current argument
            alert(arguments[i]);
    }
}

ExampleArguments('arg1',2,someVar);
Share:
71,545
Ian
Author by

Ian

Updated on August 07, 2022

Comments

  • Ian
    Ian almost 2 years

    I have two nearly identical javascript functions that are used to initiate a jquery $.get call. The arguments to the function are passed to the script being called.

    The problem is that one set of calls requires an additional argument that the other does not.

    To accomplish this I'm using the two nearly identical javascript functions I've mentioned. Here they are:

    function process(url, domid, domain, scan_id)
    {
        $.get(url,
        {
            domain: domain,
            scan_id: scan_id
        },
    
        function(data)
        {
            $(domid).html(data);
        });
    }
    
    function process_type(url, domid, type, domain, scan_id)
    {
        $.get(url,
        {
            domain: domain,
            type: type,
            scan_id: scan_id
        },
    
        function(data)
        {
            $(domid).html(data);
        });
    }
    

    As you can see, the 2nd function merely accepts an additional argument called 'type' which is then passed through the $.get call.

    I want to combine these two functions, but I'm not sure how I can optionally include that 3rd argument in that ( array / object / whatever it is in { } ( yes, javascript noob) ) that's being passed in $.get.

    EDIT just to say.... damn, you guys are good. :D

  • Dijar
    Dijar over 15 years
    You'd have to put your optional parameter on the end of the parameter list so all of them have fixed numbers, then you can access them by arguments[x] instead by their names.
  • Ian
    Ian over 15 years
    thanks for the education... slowly but surely, it's all seeping in. ;)
  • Quintin Robinson
    Quintin Robinson over 15 years
    Heh that's all it's meant for, I hope it helps!