关于jquery:Javascript函数和可选参数

Javascript Functions and optional arguments

我有两个几乎相同的javascript函数,用于启动jquery$.get调用。函数的参数将传递给正在调用的脚本。

问题是一组调用需要另一个不需要的参数。

为了实现这一点,我使用了两个几乎相同的javascript函数。它们在这里:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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调用传递该参数。

我想把这两个函数结合起来,但我不知道如何选择将第三个参数(array/object/无论它在(是的,javascript noob))包含在正在以$.get传递的参数中。

编辑只是为了说…该死,你们很好。D


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

1
2
3
4
5
6
7
8
function myFunction(option1)
{
   var option2 = arguments[1];
   if(arguments[0] == option1)
      alert("Happy Day, Option1 =" + option1 +", Option2 =" + option2);
}

myFunction("Hello","World");

产生:快乐的一天,选项1=你好,选项2=世界

希望这说明了如何使用arguments数组来改进一些代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    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);
            });
    }

然后,您可以使用最后一个参数作为可选类型来调用它,如果传递了参数,则使用它,如果不传递,则忽略它。

另外,由于实际参数在第一个地方是可选的,所以您也可以在函数定义的末尾添加名称,并使用相同的if,而不是arguments[4],您可以使用if(type) myOptions["type"] = type;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    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);
            });
    }

此呼叫将包括类型

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

这个电话打不到的地方

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


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

1
2
3
4
5
6
7
8
9
10
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:'..'});


一些简单的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// '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",例如:

1
2
3
4
5
6
7
8
function ExampleArguments(){
    for(var i=0; i<arguments.length; i++){
            // Alert the current argument
            alert(arguments[i]);
    }
}

ExampleArguments('arg1',2,someVar);