By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,899 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,899 IT Pros & Developers. It's quick & easy.

How can function A call function B with the same argument list?

P: n/a
I have function A, which need to call function B with the exact same
argument list. What is the correct way to do this?

I had function A:

function A {
B(arguments);
}

but it does not work.

I had function A:

function A {
// code to copy the argument list to another array arr
B(arr);
}

but that does not work neither. In both cases, it tell me that
function B is expecting different number of arguments.

What I am doing wrong?

Feb 13 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Feb 13, 12:08 pm, "Khai Doan" <khaibri...@gmail.comwrote:
I have function A, which need to call function B with the exact same
argument list. What is the correct way to do this?

I had function A:

function A {
B(arguments);

}

but it does not work.

I had function A:

function A {
// code to copy the argument list to another array arr
B(arr);

}

but that does not work neither. In both cases, it tell me that
function B is expecting different number of arguments.

What I am doing wrong?
You seek the Function's apply method. The first argument is the
object to use for the called function's this keyword (I've used null,
which will result it in being set to the global/window object), the
second is a list object to use for the arguments object (it can be an
Array or other list, such as the arguments object of the calling
function):

function b(){
for (var i=0; i<arguments.length; i++){
alert('arg ' + i + ': ' + arguments[i]);
}
}

function a(){
b.apply(null, arguments);
}

a('fred', 'Sally', 'Jo');

I should probably point you to the ECMAScript Language spec, but I'll
let you find that (it's in the section with the built-in objects).
Here's one for Mozilla.org's Core JavaScript 1.5 Reference:

<URL: http://developer.mozilla.org/en/docs/
Core_JavaScript_1.5_Reference:Global_Objects:Funct ion:apply >
--
Rob

Feb 13 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.