我正在寻找一个窍门。我知道如何在JavaScript中调用动态的任意函数,并传递特定的参数,如下所示:
function mainfunc(func, par1, par2){
window[func](par1, par2);
}
function calledfunc(par1, par2){
// Do stuff here
}
mainfunc('calledfunc', 'hello', 'bye');
我知道如何使用arguments
内部的集合传递可选的,无限制的参数mainfunc
,但是,我无法确定如何发送任意数量的参数mainfunc
以calledfunc
动态发送给它;我怎么能做到这样的事情,但与任意数量的可选参数(不使用那个丑陋if
- else
)?
function mainfunc(func){
if(arguments.length == 3)
window[func](arguments[1], arguments[2]);
else if(arguments.length == 4)
window[func](arguments[1], arguments[2], arguments[3]);
else if(arguments.length == 5)
window[func](arguments[1], arguments[2], arguments[3], arguments[4]);
}
function calledfunc1(par1, par2){
// Do stuff here
}
function calledfunc2(par1, par2, par3){
// Do stuff here
}
mainfunc('calledfunc1', 'hello', 'bye');
mainfunc('calledfunc2', 'hello', 'bye', 'goodbye');
使用函数的apply方法:-
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");
您的代码仅适用于全局函数,即。window
对象的成员。要将其与任意函数一起使用,请将函数本身而不是其名称作为字符串传递:
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"
你可以用 .apply()
你需要指定一个this
......我想你可以使用的this
范围内mainfunc
。
function mainfunc (func)
{
var args = new Array();
for (var i = 1; i < arguments.length; i++)
args.push(arguments[i]);
window[func].apply(this, args);
}
这是您需要的:
function mainfunc (){
window[Array.prototype.shift.call(arguments)].apply(null, arguments);
}
第一个参数用作函数名称,其余所有参数用作被调用函数的参数...
我们能够使用该shift
方法返回,然后从arguments数组中删除第一个值。请注意,我们从Array原型中调用了它,因为严格来讲,“参数”不是真正的数组,因此不会shift
像常规数组那样继承该方法。
您也可以像这样调用shift方法:
[].shift.call(arguments);
最简单的方法可能是:
var func='myDynamicFunction_'+myHandler;
var arg1 = 100, arg2 = 'abc';
window[func].apply(null,[arg1, arg2]);
假设该目标函数已经附加到“窗口”对象。
如果要与其他一些“参数”一起传递,则必须一起创建所有参数的数组,即:
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);
}
}
现在我正在使用这个:
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();
}
}
}
});
};
function a(a, b) {
return a + b
};
function call_a() {
return a.apply(a, Array.prototype.slice.call(arguments, 0));
}
console.log(call_a(1, 2))
控制台:3
您不能只传递arguments
数组吗?
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');
如果有人仍在寻找带有动态参数的动态函数调用-
callFunction("aaa('hello', 'world')");
function callFunction(func) {
try
{
eval(func);
}
catch (e)
{ }
}
function aaa(a, b) {
alert(a + ' ' + b);
}
本文地址:http://javascript.askforanswer.com/shiyongdongtaishuliangdecanshudiaoyongdongtaihanshuzhongfu.html
文章标签:function , javascript
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
文章标签:function , javascript
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!