繁体   English   中英

将多个参数传递给 Javascript 函数

[英]Passing multiple parameters to a Javascript function

我有一个功能如下:

//Calling the Function with one Parameter
responses(baseURL);

//Function Definition
function responses(baseURL) {
    $.ajax({
        url: baseURL,
        type: "get",
        cache: false,
        headers: {
            'Content-Type': 'application/json'
        },
        success: function (data) {
            console.log(data.features.length);
            for (var i = 0; i < data.features.length; i++) {
                if (taxArrayT1.indexOf(data.features[i].properties.taxon_id) == -1) {
                    taxArrayT1.push(data.features[i].properties.taxon_id);
                }
            }
            console.log("In the Invertebrate Animals Section 1");
            console.log(taxArrayT1.length);
        }
    })
}

现在我倾向于重复自己,因为当我使用相同的功能访问不同的服务时。 我知道如何将基本 URL 作为参数传递。 还有一个类似于本例中的数组taxArrayT1 每次使用不同的输入时,此数组都会更改,例如taxArrayT2 如果您有关于如何完成的建议,那就太好了。 会有很大帮助。

如果我正确理解您要做什么,那么您可以将数组添加为第二个参数。 像这样:

function responses(baseURL, taxArray) {
    $.ajax({
        url: baseURL,
        type: "get",
        cache: false,
        headers: {
            'Content-Type': 'application/json'
        },
        success: function (data) {
            console.log(data.features.length);
            for (var i = 0; i < data.features.length; i++) {
                if (taxArray.indexOf(data.features[i].properties.taxon_id) == -1) {
                    taxArray.push(data.features[i].properties.taxon_id);
                }
            }
            console.log("In the Invertebrate Animals Section 1");
            console.log(taxArray.length);
        }
    })
}

服务调用将如下所示:

responses(url1, taxArrayT1);
responses(url2, taxArrayT1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM