[英]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.