calling a function with parameters packed in array

  • Thread starter Sergey Emantayev
  • Start date
S

Sergey Emantayev

Hi everyone,

I need to call a 3rd party's function which accepts a variable number
of arguments. I have the parameters packed in an array so I need to
call it like Func(params[0], params[1], params[2]...). The problem is
that the array may have a different length and I need to pass the
array somehow instead of comma delimeted param list.
I have a nasty solution by passing a dynamically generated call string
to eval( ) like following:

var params = ... // an array containing the function arguments.

var s = 'Func(';
for (i in params)
{
if (i > 0)
{
s += ',';
}
s += "'"+params+"'";
}
s += ')';
eval(s);

Is there a more elegant way to fix that?
Like (this does not work...) -

Func.arguments = params;
Func();

Regards,
Sergey Emantayev
 
T

Thomas 'PointedEars' Lahn

Sergey said:
I need to call a 3rd party's function which accepts a variable number
of arguments. I have the parameters packed in an array so I need to
call it like Func(params[0], params[1], params[2]...).

That is usually unnecessary.
The problem is that the array may have a different length and I need
to pass the array somehow instead of comma delimeted param list.
I have a nasty solution by passing a dynamically generated call string
to eval( ) like following:
[...]
Is there a more elegant way to fix that?

functionRef.apply(thisRef, yourArrayLikeObject);

is supported since JavaScript 1.3, JScript 5.5, ECMAScript 3. Array-like
means here that the object needs to have numeric properties with values in
the order of the arguments.

<http://PointedEars.de/es-matrix/#f>

However, unless your method accepts an unlimited number of arguments, you do
not need eval() or Function.prototype.apply(). You can usually pass all
named arguments always (which will be initialized with `undefined' if not
passed), or you can use switch...case...default or if..else if...else to
make different calls depending on the number of passed arguments.
Like (this does not work...) -

Func.arguments = params;
Func();

Fantasy coding seldom yields viable results.


PointedEars
 
D

dhtml

Thomas said:
Sergey said:
I need to call a 3rd party's function which accepts a variable number
of arguments. I have the parameters packed in an array so I need to
call it like Func(params[0], params[1], params[2]...).

That is usually unnecessary.
The problem is that the array may have a different length and I need
to pass the array somehow instead of comma delimeted param list.
I have a nasty solution by passing a dynamically generated call string
to eval( ) like following:
[...]
Is there a more elegant way to fix that?

functionRef.apply(thisRef, yourArrayLikeObject);

is supported since JavaScript 1.3, JScript 5.5, ECMAScript 3. Array-like
means here that the object needs to have numeric properties with values in
the order of the arguments.

It does not work that way.

Function.prototype.apply takes, for the second parameter, an Array or an
arguments object. Anything else should throw a TypeError[1].

Function.prototype.apply can be used to solve the OP's situation, so
long as he has an array or arguments object.

http://bclary.com/2004/11/07/#a-15.3.4.3
 
S

Sergey Emantayev

Thank you all!
That helped me a lot.

Sergey

Thomas said:
Sergey said:
I need to call a 3rd party's function which accepts a variable number
of arguments. I have the parameters packed in an array so I need to
call it like Func(params[0], params[1], params[2]...).
That is usually unnecessary.
The problem is that the array may have a different length and I need
to pass the array somehow instead of comma delimeted param list.
I have a nasty solution by passing a dynamically generated call string
to eval( ) like following:
[...]
Is there a more elegant way to fix that?
  functionRef.apply(thisRef, yourArrayLikeObject);
is supported since JavaScript 1.3, JScript 5.5, ECMAScript 3.  Array-like
means here that the object needs to have numeric properties with valuesin
the order of the arguments.

It does not work that way.

Function.prototype.apply takes, for the second parameter, an Array or an
arguments object. Anything else should throw a TypeError[1].

Function.prototype.apply can be used to solve the OP's situation, so
long as he has an array or arguments object.

http://bclary.com/2004/11/07/#a-15.3.4.3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top