Javascript 使用动态参数数调用动态函数
我在找一个技巧。我知道如何在JavaScript中调用动态的任意函数,传递特定的参数,比如:Javascript 使用动态参数数调用动态函数,javascript,function,Javascript,Function,我在找一个技巧。我知道如何在JavaScript中调用动态的任意函数,传递特定的参数,比如: function mainfunc(func, par1, par2){ window[func](par1, par2); } function calledfunc(par1, par2){ // Do stuff here } mainfunc('calledfunc', 'hello', 'bye'); [].shift.call(arguments); var Log
function mainfunc(func, par1, par2){
window[func](par1, par2);
}
function calledfunc(par1, par2){
// Do stuff here
}
mainfunc('calledfunc', 'hello', 'bye');
[].shift.call(arguments);
var Log = {
log: function() {
var args = ['myarg here'];
for(i=0; i<arguments.length; i++) args = args.concat(arguments[i]);
console.log.apply(this, args);
}
}
我知道如何使用mainfunc
中的arguments
集合传递可选的、不受限制的参数,但是,我不知道如何将任意数量的参数发送到mainfunc
,以便动态发送到calledfunc
;我怎样才能完成这样的事情,但是使用任意数量的可选参数(不使用丑陋的if
–else
)
使用函数的应用方法:-
function mainfunc (func){
window[func].apply(null, Array.prototype.slice.call(arguments, 1));
}
编辑:我觉得稍微调整一下会更有用:-
function mainfunc (func){
this[func].apply(this, Array.prototype.slice.call(arguments, 1));
}
这将在浏览器之外工作(This
默认为全局空间)。在mainfunc上使用call也可以:-
function target(a) {
alert(a)
}
var o = {
suffix: " World",
target: function(s) { alert(s + this.suffix); }
};
mainfunc("target", "Hello");
mainfunc.call(o, "target", "Hello");
你不能把
参数
数组传下去吗
function mainfunc (func){
// remove the first argument containing the function name
arguments.shift();
window[func].apply(null, arguments);
}
function calledfunc1(args){
// Do stuff here
}
function calledfunc2(args){
// Do stuff here
}
mainfunc('calledfunc1','hello','bye');
mainfunc('calledfunc2','hello','bye','goodbye');
你可以用
您需要为此指定一个。。。我想您可以在mainfunc
中使用this
function mainfunc (func)
{
var args = new Array();
for (var i = 1; i < arguments.length; i++)
args.push(arguments[i]);
window[func].apply(this, args);
}
函数mainfunc(func)
{
var args=新数组();
for(var i=1;i
您的代码仅适用于全局函数,即窗口
对象的成员。要将其用于任意函数,请将函数本身而不是其名称作为字符串传递:
function dispatch(fn, args) {
fn = (typeof fn == "function") ? fn : window[fn]; // Allow fn to be a function object or the name of a global function
return fn.apply(this, args || []); // args is optional, use an empty array by default
}
function f1() {}
function f2() {
var f = function() {};
dispatch(f, [1, 2, 3]);
}
dispatch(f1, ["foobar"]);
dispatch("f1", ["foobar"]);
f2(); // calls inner-function "f" in "f2"
dispatch("f", [1, 2, 3]); // doesn't work since "f" is local in "f2"
以下是您需要的:
function mainfunc (){
window[Array.prototype.shift.call(arguments)].apply(null, arguments);
}
第一个参数用作函数名,其余所有参数用作被调用函数的参数
我们可以使用shift
方法返回并删除arguments数组中的第一个值。请注意,我们从数组原型调用它,因为严格来说,“arguments”不是一个真正的数组,因此不像常规数组那样继承shift
方法
您也可以这样调用shift方法:
function mainfunc(func, par1, par2){
window[func](par1, par2);
}
function calledfunc(par1, par2){
// Do stuff here
}
mainfunc('calledfunc', 'hello', 'bye');
[].shift.call(arguments);
var Log = {
log: function() {
var args = ['myarg here'];
for(i=0; i<arguments.length; i++) args = args.concat(arguments[i]);
console.log.apply(this, args);
}
}
如果要传递其他几个“参数”,则必须一起创建所有参数的数组,即如下所示:
function mainfunc(func, par1, par2){
window[func](par1, par2);
}
function calledfunc(par1, par2){
// Do stuff here
}
mainfunc('calledfunc', 'hello', 'bye');
[].shift.call(arguments);
var Log = {
log: function() {
var args = ['myarg here'];
for(i=0; i<arguments.length; i++) args = args.concat(arguments[i]);
console.log.apply(this, args);
}
}
var日志={
日志:函数(){
var args=['myarg here'];
对于(i=0;i而言,最简单的方法可能是:
var func='myDynamicFunction_'+myHandler;
var arg1 = 100, arg2 = 'abc';
window[func].apply(null,[arg1, arg2]);
假设目标函数已附加到“窗口”对象。如果有人仍在寻找具有动态参数的动态函数调用-
callFunction("aaa('hello', 'world')");
function callFunction(func) {
try
{
eval(func);
}
catch (e)
{ }
}
function aaa(a, b) {
alert(a + ' ' + b);
}
现在我用这个:
Dialoglar.Confirm = function (_title, _question, callback_OK) {
var confirmArguments = arguments;
bootbox.dialog({
title: "<b>" + _title + "</b>",
message: _question,
buttons: {
success: {
label: "OK",
className: "btn-success",
callback: function () {
if (typeof(callback_OK) == "function") { callback_OK.apply(this,Array.prototype.slice.call(confirmArguments, 3));
}
}
},
danger: {
label: "Cancel",
className: "btn-danger",
callback: function () {
$(this).hide();
}
}
}
});
};
Dialoglar.Confirm=函数(\u标题,\u问题,回调\u确定){
var confirmArguments=参数;
bootbox.dialog({
标题:“+”标题+“”,
留言:"问题,,
按钮:{
成功:{
标签:“OK”,
类名:“btn成功”,
回调:函数(){
if(typeof(callback_OK)=“function”){callback_OK.apply(this,Array.prototype.slice.call(confirmArguments,3));
}
}
},
危险:{
标签:“取消”,
类名:“btn危险”,
回调:函数(){
$(this.hide();
}
}
}
});
};
控制台:3
先告诉我:)要获得更详细的文档,这是最合适的解决方案,但在参数中它将包括函数名(参数[0]=“func”)。我可以使用除第一个参数外的所有参数生成另一个数组,然后使用此新数组,但是,是否有其他解决方案不使用其他数组?ARemesal,请参阅我的答案以获得解决方案(使用第一个参数作为函数名)@ARemesal:Oops是的,忘了从参数中切掉func参数。请参见我的编辑。是的,AnthonyWJones,它相当于JimmyP的解决方案。感谢你的解决方案,我发现了一些很棒的技巧:)不,你不能,因为“arguments”不是数组,而是类似数组的对象,并且没有shift()您需要将参数1复制到arguments.length到新数组中,并将该数组传递给apply()。这是可行的,但你应该注意,这不是要求的。你不需要将“参数”转换为真正的数组。这有什么意义?如果你不想省略第一个参数,即函数的名称,它就可以完美地工作……为什么不直接使用“shift”呢?(参见我的答案)我没有使用shift,因为我不认为[].xxx.call在IE中工作,但我现在无法重现错误。非常感谢JimmyP,您的解决方案(编辑后)与AnthonyWJones的解决方案相当,并且您给了我关于使用Array.prototype和[]的非常好的提示和解释。奇怪的是,我肯定我尝试过这个,并且在IET中遇到了一个错误
是调用用户函数数组()
。解决方案也使用了它。不,不,不,不。而且,不。2021年,这是10倍的搞笑!