简体   繁体   English

允许javascript函数接受任意数量的参数

[英]Allowing javascript function to accept any number of arguments

I would like the below function to be more flexible and accept multiple callbacks to other functions if they are defined in the arguments. 我希望下面的函数更灵活,并接受多个回调到其他函数,如果它们在参数中定义。

$(function() {
    function icisDashBox(colorElem, thisWidth, thisHeight, completeCallBack) {
        $(colorElem).colorbox({
        transition: 'none',
        innerWidth: thisWidth,
        innerHeight: thisHeight,
        opacity: '0.5',
        onOpen: function() { 

        },
        onLoad: function() { 

        },
        onComplete:function() { 
            $('#cboxLoadedContent').wrap('<div id="icis_dialog_msg" />'); 

            completeCallBack();

        },
        onCleanup: function() { 

        },      
        onClosed: function() {
            $('#cboxLoadedContent').unwrap(); 
        }
    });
}

icisDashBox('.example9', '500', '500', completeFunction);

function completeFunction() {

    var fooClass = $("#colorbox").addClass("FOO");

    var barClass = $("#colorbox").addClass("BAR");

    var ajaxCnt = $.ajax({
        type: "GET",
        url: "http://www.payso.me.uk",
        dataType: "html",
        success: function(data) {
            $("#colorbox").addClass("AJAX SUCCESS");
        }
    });

    return {
        x : fooClass,
        y : barClass,
        z : ajaxCnt                         
    };
}

So in an ideal world my function would look like this without explicitly declaring any arguments: 所以在一个理想的世界中,我的函数看起来像这样,没有明确声明任何参数:

function icisDashBox() { function code here }

Is this possible? 这可能吗? Also if the arguments are not defined how do i handle that? 此外,如果参数未定义,我该如何处理?

For example if one call to the function has several callbacks defined and another only has one is there a way of handling the lack of presence of callbacks. 例如,如果对函数的一次调用定义了几个回调,而另一个只有一个回调,则有一种方法可以处理缺少回调的问题。

Cheers, 干杯,

:) :)

You can use the keyword arguments which is an array of the passed arguments, like this: 您可以使用关键字arguments ,它是传递的参数的数组,如下所示:

function myFunc() {
   if(arguments.length > 0)     //be sure to check if there are any...
     var arg1 = arguments[0];
}

However , a much better approach is to accept an object, eg: 但是 ,更好的方法是接受一个对象,例如:

function myFunc(settings) {
   settings = settings || {};   //in case it was called as just: myfunc()
   var something = settings.something || "default value";
}

You'd call it like this: 你这样称呼它:

myFunc({ something: "value", somethingElse: "otherValue" });

This approach allows you to accept any number of arguments as well, but also have any optional, without a bunch of myFunc(null, null, null, "value") type calls to provide only the parameter you want, plus they're named making this much more maintainable IMO. 这种方法允许你接受任意数量的参数,但也有任何可选的,没有一堆myFunc(null, null, null, "value")类型调用只提供你想要的参数,而且它们被命名这使得容易维护IMO。 Here's an edited version of the plugin to demonstrate this . 这是该插件的编辑版本,用于演示此内容

Use arguments variable. 使用参数变量。

function TestMe()
{
   var args = arguments;

   for (var a in args)
   {
     alert(args[a]);
   }
}

Now you can pass any number of arguments to TestMe function: 现在您可以将任意数量的参数传递给TestMe函数:

TestMe(1);
TestMe(1,2,3);
TestMe(1,2,3,4,5,6);
TestMe.apply(this, [1,2,3,4,5]); 

etc. 等等

Thanks to the spread operator now you can do something like this 感谢传播操作员现在你可以做这样的事情

 function myFunction(...params) { console.log(...params); } myFunction('Many arguments', 1, {two: 3}, ["four", 5]); // to access one by one function oneByOne(...params) { params.map(param => { console.log(param); }); } oneByOne('Many arguments', 1, {two: 3}, ["four", 5]); 

Yes this is possible, here's an example : 是的,这是可能的,这是一个例子:

function myfunct()
{

  var arguments = myfunct.arguments;

  for (var i = 0; i < arguments.length; i++)
        {

                alert("Argument " + i + " value = " + arguments[i]);

            }

}

You can call this fonction by any number of arguments : 您可以通过任意数量的参数调用此函数:

myfunct("foo");

myfunct("foo","bar");

在最近的时间你现在可以使用或传播和休息运算符在这里详细说明 - 将未知数量的参数传递给javascript函数

暂无
暂无

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

相关问题 Javascript-将回调函数作为参数传递,可以接受任意数量的帐户 - Javascript - passing callback function as argument that would accept any number of accounts 如何改进 Javascript 中的这个 function 可以接受任意数量的参数? - How to improve this function in Javascript which will accept any number of argument? JavaScript 函数可以接受的参数有最大数量吗? - Is there a max number of arguments JavaScript functions can accept? 在 JavaScript 中写一个 function 接受三个数字作为 arguments 并显示最大的数字 - Write a function in JavaScript which accept three numbers as arguments and display the greatest number 将任意数量的 arguments 传递给 function - Pass any number of arguments to a function javascript:如何创建一个 function 接收任意数量的 arguments 并根据数量取平均值。 arguments 的? - javascript: how to create a function that receives any number of arguments and take average depending on the num. of arguments? 具有大量参数的Javascript函数 - Javascript function with large number of arguments JavaScript 可变数量的函数参数 - JavaScript variable number of arguments to function JavaScript:创建一个函数defineFirstArg,它接受一个函数和一个参数; 接受更多参数 - JavaScript: Create a function defineFirstArg that accepts a function and an argument; Accept More Arguments 使函数接受可变数量的参数或数组中的值 - Make function accept both variable number of arguments, or values in array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM