簡體   English   中英

Javascript函數和可選參數

[英]Javascript Functions and optional arguments

我有兩個幾乎相同的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調用傳遞。

我想要結合這兩個函數,但是我不知道如何可以選擇包含第3個參數(數組/對象/它在{}中的任何內容(是的,javascript noob))在$ .get中傳遞。

編輯只是說....該死的,你們好。 :d

javascript中的所有參數都是可選的,你可以使用函數內部的參數數組來訪問通常傳遞的參數,如下所示:

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

myFunction("Hello", "World");

產生:快樂的一天,選項1 =你好,選項2 =世界

希望這說明了如何使用arguments數組來改進代碼。

    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;

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

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

一些簡單的方法來做到這一點

// '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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM