invoking a function with variable number of arguments

Discussion in 'Javascript' started by laredotornado, Jun 16, 2008.

  1. Hi,

    I'm passing a function, "myFunc" as an argument to another function,
    "doFunc". In "doFunc", I am creating an arbitrary number of arguments
    to pass to "myFunc". How do I invoke "myFunc" with an arguments array
    stored in "argsArr"? Here is the basic function skeleton:

    function doFunc(myFunc) {
    var argsArr = getArgsArray();
    // invoke myFunc with argsArr ... how?
    }

    Thanks for the help, - Dave
    laredotornado, Jun 16, 2008
    #1
    1. Advertising

  2. laredotornado

    VK Guest

    On Jun 17, 1:55 am, laredotornado <> wrote:
    > Hi,
    >
    > I'm passing a function, "myFunc" as an argument to another function,
    > "doFunc". In "doFunc", I am creating an arbitrary number of arguments
    > to pass to "myFunc". How do I invoke "myFunc" with an arguments array
    > stored in "argsArr"? Here is the basic function skeleton:
    >
    > function doFunc(myFunc) {
    > var argsArr = getArgsArray();
    > // invoke myFunc with argsArr ... how?
    >
    > }


    function doFunc(myFunc) {
    myFunc.apply(null,getArgsArray());
    }

    function getArgsArray() {
    return [1,2,3];
    }

    function f() {
    for (var i=0; i<arguments.length; i++) {
    window.alert(arguments);
    }
    }

    doFunc(f);
    VK, Jun 16, 2008
    #2
    1. Advertising

  3. laredotornado

    RobG Guest

    On Jun 17, 7:55 am, laredotornado <> wrote:
    > Hi,
    >
    > I'm passing a function, "myFunc" as an argument to another function,
    > "doFunc". In "doFunc", I am creating an arbitrary number of arguments
    > to pass to "myFunc". How do I invoke "myFunc" with an arguments array
    > stored in "argsArr"? Here is the basic function skeleton:
    >
    > function doFunc(myFunc) {
    > var argsArr = getArgsArray();
    > // invoke myFunc with argsArr ... how?


    Use Function.prototype.apply:

    myFunc.apply(window, argsArr);


    <URL: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function:apply
    >


    >
    > }



    --
    Rob
    RobG, Jun 16, 2008
    #3
  4. RobG wrote:
    > On Jun 17, 7:55 am, laredotornado <> wrote:
    >> I'm passing a function, "myFunc" as an argument to another function,
    >> "doFunc". In "doFunc", I am creating an arbitrary number of arguments
    >> to pass to "myFunc". How do I invoke "myFunc" with an arguments array
    >> stored in "argsArr"? Here is the basic function skeleton:
    >>
    >> function doFunc(myFunc) {
    >> var argsArr = getArgsArray();
    >> // invoke myFunc with argsArr ... how?

    >
    > Use Function.prototype.apply:
    >
    > myFunc.apply(window, argsArr);


    Needlessly proprietary and error-prone. Should be

    myFunc.apply(this, argsArr);

    in global context and

    myFunc.apply(global, argsArr);

    in local context with `global' assigned as

    var global = this;

    in global context before.


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
    Thomas 'PointedEars' Lahn, Jun 17, 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. Alex
    Replies:
    1
    Views:
    372
    Ron Natalie
    Oct 16, 2003
  2. Vince
    Replies:
    3
    Views:
    381
    Victor Bazarov
    Nov 10, 2004
  3. jmborr
    Replies:
    1
    Views:
    412
    Stargaming
    Nov 3, 2007
  4. Replies:
    3
    Views:
    821
  5. Navaneeth
    Replies:
    4
    Views:
    550
    Kenny McCormack
    Nov 20, 2010
Loading...

Share This Page