473,657 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2996
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.mozil la.org/en/docs/Core_JavaScript _1.5_Reference: Global_Objects: Function: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.mozil la.org/en/docs/Core_JavaScript _1.5_Reference: Global_Objects: Function: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.mozil la.org/en/docs/Core_JavaScript _1.5_Reference: Global_Objects: Function: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.javas cript message
<11************ **********@73g2 000cwn.googlegr oups.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.demo n.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.co m/CompLangPascalD elphiMisc-MiniFAQ.htmclpd mFAQ;
<URL:http://www.borland.com/newsgroups/guide.htmlnews: borland.* Guidelines
Dec 19 '06 #5
Dr J R Stockton schreef:
In comp.lang.javas cript message
<11************ **********@73g2 000cwn.googlegr oups.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
8307
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 reference?? Thanks, BB
5
6371
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 array.split(";") command and proceeded to update the elemment by assigning the null value to the arrayindex array=""
7
25166
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 (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
4
1781
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. thanks!! ============================================ void reverse(char s); void count(int x);
3
2828
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 <string.h> double data={{1.0, 3.0},{9.0, 8.0}};
11
3769
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
20387
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 passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
3
2495
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
2053
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 = args.shift(); return function(){ return fn.apply(object, args.concat(Array.prototype.slice.call(arguments)));
0
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.