...

View Full Version : How to Set Function Name as Argument of Another Function?



dealmaker
12-14-2005, 06:43 AM
Hi,
I want to pass a function A name as an argument to function B because I need to use different functions to do something in function B. At the same time, I am using the arguments[] in function B because I am don't know how many arguments will be in the function B. I also need the arguments[0]() to be able to accept any # of arguments.
e.g.



function B() {
var data;
arguments[0](data);
}

B(A);


It doesn't seem to work. I need it to work in both IE and firefox.

Many thanks.

liorean
12-14-2005, 06:58 AM
arguments[0](data);You're missing an 's' there.

dealmaker
12-14-2005, 07:03 AM
Actually, I have a 's' in the actual code, it doesn't work. I just mistyped in this thread, and I corrected it.


You're missing an 's' there.

liorean
12-14-2005, 07:14 AM
Well, this works:
function B(){
var
data='b>data',
i=0,
l=arguments.length;
while(i<l)
arguments[i++](data);
}

function A(s){
alert(s);
}

B(A);

dealmaker
12-14-2005, 07:23 AM
Actually, only one of the arguments of B is function, the rest are just variables. And what is 'b>data'? Is it a typo? And I need arguments(data) to accept any # of variables, not just one variable.


Well, this works:
function B(){
var
data='b>data',
i=0,
l=arguments.length;
while(i<l)
arguments[i++](data);
}

function A(s){
alert(s);
}

B(A);

liorean
12-14-2005, 08:04 AM
Actually, only one of the arguments of B is function, the rest are just variables. And what is 'b>data'? Is it a typo? And I need arguments(data) to accept any # of variables, not just one variable.
You declared the data variable yourself in your example. I just gave it a value to show you that it does, in fact, work.


function B(fn){
var
a=[],
i=1,
l=arguments.length;
while(i<l)
a.push(arguments[i++]);
return fn?
fn.apply(this,a):
undefined;
}

function A(){
var
a=[],
i=0,
l=arguments.length;
while(i<l)
a.push(arguments[i++]);
return('Arguments to A:\n----\n'+a.join(',\n'));
}

alert(B(A,0,1,2,3,4));
alert(B(A));
alert(B());



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum