允许javascript函数接受任意数量的参数 [英] Allowing javascript function to accept any number of arguments

查看:115
本文介绍了允许javascript函数接受任意数量的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望下面的函数更加灵活,并且如果在参数中定义了多个回调函数,则接受多个回调函数。

  $(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);
$ b函数completeFunction(){

var fooClass = $(#colorbox)。addClass(FOO);

var barClass = $(#colorbox)。addClass(BAR);

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

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

所以在一个理想的世界中,我的函数看起来像这样,而不显式声明任何参数:

 函数icisDashBox(){此处的函数代码} 

这可能吗?如果参数未定义,我该如何处理?



例如,如果对该函数的一次调用定义了多个回调函数,而另一个回调函数只有一个回调函数,处理缺乏回调的存在。



干杯,
$ b

解决方案

您可以使用关键字 arguments ,它是传递参数的数组,如下所示:

  function myFunc(){
if(arguments.length> 0)//确保检查是否有任何...
var arg1 = arguments [0];
}

然而,更好的方法是接受一个对象,例如:

  function myFunc(settings){
settings = settings || {}; //如果它被称为只是:myfunc()
var something = settings.something || 默认值;
}

你会这样称呼它:

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

这种方法允许您接受任意数量的参数,但也有任何可选的,没有一堆 myFunc(null,null,null,value)类型调用只提供你想要的参数,再加上他们命名为更加可维护的IMO。 这里有一个插件的编辑版本来演示


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:

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, e.g.:

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. Here's an edited version of the plugin to demonstrate this.

这篇关于允许javascript函数接受任意数量的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆