How to add parameters to a function passed as a paramter?

Discussion in 'Javascript' started by Steve Neill, Oct 26, 2004.

  1. Steve Neill

    Steve Neill Guest

    Here's an interesting problem...

    I have 2 functions, aFunc() and bFunc(). I call aFunc() passing a call
    to bFunc() as a parameter. Function aFunc() executes bFunc(). Function
    bFunc() lists the parameter values passed to it.

    OK, here's the twist...

    I want to add some parameters to bFunc() from inside aFunc(). Can that
    be done?

    Sorry no prizes, except the satisfaction of the solution :)

    Regards,
    Steve


    aFunc(function() { bFunc(); } );

    function aFunc(f) {
    var x = 1;
    var y = 2;

    // add x, y as parameters to function f()
    // ???

    // execute the parameter function
    f();
    }

    function bFunc() {
    for (var i = 0; i < arguments.length; ++i) {
    alert(arguments);
    }
    }
     
    Steve Neill, Oct 26, 2004
    #1
    1. Advertising

  2. On 26 Oct 2004 14:21:29 -0700, Steve Neill <> wrote:

    > I have 2 functions, aFunc() and bFunc(). I call aFunc() passing a call
    > to bFunc() as a parameter. Function aFunc() executes bFunc(). Function
    > bFunc() lists the parameter values passed to it.
    >
    > OK, here's the twist...
    >
    > I want to add some parameters to bFunc() from inside aFunc(). Can that
    > be done?


    Yes, but your call to aFunc needs to be changed.

    Functions are objects, and just like all other objects, you can refer to
    them with any number of identifiers. Consider:

    function myFunction() {
    }

    var yourFunction = myFunction;

    You can now invoke myFunction with either myFunction() or yourFunction()
    with exactly the same results. For your particular case, this would become:

    function aFunc(f) {
    var x = 1,
    y = 2;

    f(x, y); // Call bFunc with arguments x and y.
    }

    function bFunc() {
    for(var i = 0, n = arguments.length; i < n; ++i) {
    alert(arguments);
    }
    }

    aFunc(bFunc); // Note: no parentheses on bFunc!

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Oct 27, 2004
    #2
    1. Advertising

  3. Steve Neill

    Bob Gregory Guest

    (Steve Neill) wrote in message news:<>...
    > Here's an interesting problem...
    >

    <snip />

    Am I missing something?

    aFunc(bFunc);

    function aFunc(f) {
    var x = 1;
    var y = 2;

    // add x, y as parameters to function f()
    // ???

    // execute the parameter function
    f(x,y);
    }

    function bFunc() {
    for (var i = 0; i < bFunc.arguments.length; ++i) {
    alert(bFunc.arguments);
    }
    }
     
    Bob Gregory, Oct 27, 2004
    #3
  4. On 27 Oct 2004 06:12:14 -0700, Bob Gregory <> wrote:

    [snip]

    > Am I missing something?


    Not especially, but...

    [snip]

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


    ....the arguments object is not a property of the function, but of the
    function's activation object. Effectively, it is a local variable to
    accessed like all other local variables.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Oct 27, 2004
    #4
  5. Steve Neill

    Steve Neill Guest

    Thank you Michael. I didn't realize I could pass a function that way
    (now I do!) This solves the problem -- Thank you
     
    Steve Neill, Oct 27, 2004
    #5
    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. Randy

    Grabbing paramter in the URL

    Randy, Feb 8, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    373
    Eliyahu Goldin
    Feb 8, 2005
  2. dee
    Replies:
    9
    Views:
    541
    Joseph Byrns
    Apr 15, 2005
  3. tshad
    Replies:
    5
    Views:
    564
    Steve C. Orr [MVP, MCSD]
    May 17, 2005
  4. Anand
    Replies:
    2
    Views:
    938
    Anand
    Sep 11, 2003
  5. Why Tea

    Array passed as function parameters

    Why Tea, Aug 16, 2012, in forum: C Programming
    Replies:
    21
    Views:
    795
    ais523
    Aug 26, 2012
Loading...

Share This Page