473,408 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Pass array elements as function arguments

I have a function which can be called with an unlimited number of
arguments.
I want to call another function with exactly the same arguments. I know
I can get the arguments in the arguments object, and as such also in an
array, but how do you pass the elements of an array to another function
as parameters?

I hope you understand what I mean.

Thanks,
Frederik Vanderstraeten

Dec 19 '06 #1
5 2965
FrederikVds wrote:
I have a function which can be called with an unlimited number of
arguments.
I want to call another function with exactly the same arguments. I know
I can get the arguments in the arguments object, and as such also in an
array, but how do you pass the elements of an array to another function
as parameters?
Use the apply method of functions e.g.

function f () {
alert(arguments.length);
}
function g () {
f.apply(this, arguments);
}

g(1, 2, 3);

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

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 19 '06 #2
Martin Honnen wrote:
FrederikVds wrote:
>I have a function which can be called with an unlimited number of
arguments.
I want to call another function with exactly the same arguments. I know
I can get the arguments in the arguments object, and as such also in an
array, but how do you pass the elements of an array to another function
as parameters?

Use the apply method of functions e.g.

function f () {
alert(arguments.length);
}
function g () {
f.apply(this, arguments);
}

g(1, 2, 3);

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

Truly elegant.

--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Dec 19 '06 #3
Martin Honnen schreef:
FrederikVds wrote:
>I have a function which can be called with an unlimited number of
arguments.
I want to call another function with exactly the same arguments. I know
I can get the arguments in the arguments object, and as such also in an
array, but how do you pass the elements of an array to another function
as parameters?

Use the apply method of functions e.g.

function f () {
alert(arguments.length);
}
function g () {
f.apply(this, arguments);
}

g(1, 2, 3);

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

Thanks a lot, I had no idea about the existence of this function. I
should have searched at devmo instead of Google.
Dec 19 '06 #4
In comp.lang.javascript message
<11**********************@73g2000cwn.googlegroups. com>, Tue, 19 Dec 2006
07:56:16, FrederikVds <fr*********************@gmail.comwrote:
>I have a function which can be called with an unlimited number of
arguments.
I want to call another function with exactly the same arguments. I know
I can get the arguments in the arguments object, and as such also in an
array, but how do you pass the elements of an array to another function
as parameters?
Rewrite the function to take a single parameter which will be an Object
holding all the previous parameters. You can easily pass that Object in
the next call. Untested.

That should work in older browsers; subtler ways may only work in newer
ones.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3? Turnpike 6.05
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htmclpdmFAQ;
<URL:http://www.borland.com/newsgroups/guide.htmlnews:borland.* Guidelines
Dec 19 '06 #5
Dr J R Stockton schreef:
In comp.lang.javascript message
<11**********************@73g2000cwn.googlegroups. com>, Tue, 19 Dec 2006
07:56:16, FrederikVds <fr*********************@gmail.comwrote:
>I have a function which can be called with an unlimited number of
arguments.
I want to call another function with exactly the same arguments. I know
I can get the arguments in the arguments object, and as such also in an
array, but how do you pass the elements of an array to another function
as parameters?

Rewrite the function to take a single parameter which will be an Object
holding all the previous parameters. You can easily pass that Object in
the next call. Untested.

That should work in older browsers; subtler ways may only work in newer
ones.
That's not really possible as some of the functions I call may be
built-in functions.
Dec 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
5
by: effendi | last post by:
I wrote a simple script to remove an element of an array but I don't think this is the best way to go about it. I have a list of about five elements seperated by ";" I split the array using...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
4
by: jrefactors | last post by:
In the following program, are parameters s in function reverse() and x in function count() both pass by value? How come value k is not changed, but value str has changed? Please advise....
3
by: questions? | last post by:
I tried to pass a two dimensional array in the function arguments the following program is a demonstration, ******************************************** # include <stdio.h> # include...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
3
by: QQ | last post by:
I have one integer array int A; I need to pass this array into a function and evaluate this array in this function how should I pass? Is it fine? void test(int *a)
12
by: lorlarz | last post by:
In the code sample below, how are arguments a legitimate argument to Array.slice? Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.