【问题标题】:Javascript Functions and optional argumentsJavascript 函数和可选参数
【发布时间】:2010-10-09 21:43:57
【问题描述】:

我有两个几乎相同的 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.

编辑只是说....该死的,你们很好。 :D

【问题讨论】:

标签: javascript jquery arguments


【解决方案1】:

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

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

myFunction("Hello", "World");

产生:Happy Day,Option1 = Hello,Option2 = 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");

【讨论】:

  • 感谢您的教育...慢慢但肯定地,这一切都在渗入。;)
  • 嘿,这就是它的全部意义,我希望它有所帮助!
【解决方案2】:

既然除了 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:'..'});

【讨论】:

    【解决方案3】:

    一些简单的方法

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

    对于无限参数,您可以使用数组“参数”,例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 2016-03-27
      相关资源
      最近更新 更多