calling a function with parameters packed in array

Discussion in 'Javascript' started by Sergey Emantayev, Nov 2, 2008.

  1. 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
     
    Sergey Emantayev, Nov 2, 2008
    #1
    1. Advertising

  2. Sergey Emantayev wrote:
    > 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
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
     
    Thomas 'PointedEars' Lahn, Nov 2, 2008
    #2
    1. Advertising

  3. Sergey Emantayev

    dhtml Guest

    Thomas 'PointedEars' Lahn wrote:
    > Sergey Emantayev wrote:
    >> 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

    --
    comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
     
    dhtml, Nov 2, 2008
    #3
  4. Thank you all!
    That helped me a lot.

    Sergey

    On Nov 2, 11:40 pm, dhtml <> wrote:
    > Thomas 'PointedEars' Lahn wrote:
    > > Sergey Emantayev wrote:
    > >> 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
    >
    > --
    > comp.lang.javascript FAQ <URL:http://jibbering.com/faq/>
     
    Sergey Emantayev, Nov 4, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. umberto
    Replies:
    4
    Views:
    573
    umberto
    Oct 24, 2003
  2. Chris
    Replies:
    6
    Views:
    509
    Chris Uppal
    Nov 7, 2004
  3. Sameer
    Replies:
    12
    Views:
    888
    Roedy Green
    Oct 6, 2005
  4. Replies:
    3
    Views:
    803
    Robert Gamble
    Jan 18, 2006
  5. Sikandar

    Packed array in C

    Sikandar, Sep 25, 2007, in forum: C Programming
    Replies:
    9
    Views:
    571
    Richard Tobin
    Sep 26, 2007
Loading...

Share This Page