JavaScript函数和可选参数

36

我有两个几乎相同的JavaScript函数,用于启动jQuery $.get调用。传递给被调用脚本的参数将传递给该函数。

问题是其中一个调用需要另外一个参数,而另一个则不需要。

为了解决这个问题,我使用了我之前提到的这两个近乎相同的JavaScript函数。它们如下所示:

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);
    });
}

从上面可以看出,第二个函数仅接受一个名为“type”的额外参数,然后通过$.get调用传递该参数。

我想将这两个函数组合起来,但我不确定如何在传递给$.get的(数组/对象/无论是大括号{}中的任何内容(是的,我是JavaScript新手))中可选地包含第三个参数。

编辑只是想说....天哪,你们真厉害。:D


请参考此线程:https://dev59.com/knRB5IYBdhLWcg3w9Lzn。 - Chetan S
3个回答

35

JavaScript中的所有参数都是可选的,您可以在函数内部使用参数数组来访问按顺序传递的参数,例如:

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

myFunction("Hello", "World");

输出结果为:快乐的一天,选项1 = Hello,选项2 = World

希望这样说明了如何使用参数数组来改进代码。

    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);
            });
    }

如果传递了类型参数,就可以将其作为最后一个可选参数调用。如果没有传递,则会被省略。

由于实际参数本身是可选的,因此您还可以将名称添加到函数定义的末尾,并在同一位置使用它,但是不是使用 arguments[4],而是使用 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);
            });
    }

此调用将包括类型信息

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

如果调用此处不能

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

你需要将可选参数放在参数列表的末尾,这样所有参数都有固定的编号,然后你可以通过arguments[x]而不是它们的名称来访问它们。 - schnaader
感谢您的教育......虽然缓慢但一定会慢慢深入理解的。 ;) - Ian
嘿,这就是它的全部用途,希望能有所帮助! - Quintin Robinson

29

既然除了url和domid之外,你所做的一切都是将其传递给$.get,那为什么不这样做呢?

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:'..'});

18

有一些简单的方法可以做到这一点

// '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
}

对于不限数量的参数,您可以使用数组“arguments”,例如:

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

ExampleArguments('arg1',2,someVar);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接