1

I have the following function:

function foo(f, k) {
    if (f.length > 2) {
        // how do I access the third element of f? 
        // it can be a function retry() or undefined.
        // var retry = f.arguments[2]; ?? 
        // retry();
        // console.log(f.arguments) returns undefined
    } else {
        k();
    }
}

var retry = function() { console.log("hi"); };

foo(function(x, y) { console.log(x+y); }, 
    function() { console.log("hello"); });

foo(function(x, y, retry) { console.log("retry present"); },
    function() { console.log("hello"); });

I need to call that third argument if its passed in. I may have 2 or 3 arguments being passed to f. How do I access that third argument if it's present?

19
  • 2
    A parameter is nothing that can be accessed. It's a declaration of a name for the n-th argument. You can access that argument by using that name inside the function. Commented Nov 24, 2015 at 5:57
  • 1
    @philippe: No, retry is not a function. It's just a variable name. There is also a function retry in an outer scope, but it has nothing to do with the retry parameter of the anonymous function you are passing to foo as f. Commented Nov 24, 2015 at 5:58
  • 1
    "I may have 2 or 3 arguments being passed to f" - not exactly. In your current code, you're not passing any arguments to f yet. There's a function f that may accept 2 or 3 arguments for its named parameters. Having that function f, you can determine the number of named parameters (f.length), but there's nothing beyond that - there are no values to access because nobody called the function yet. Commented Nov 24, 2015 at 6:03
  • 2
    @philippe: So what do you want to access then? Do you want to get the string "retry" which is the name of the third parameter? Commented Nov 24, 2015 at 6:05
  • 1
    @philippe: You may want to re-read my explanation. There is no "third element" that is a function that could be invoked. Your request makes no sense. Commented Nov 24, 2015 at 6:07

1 Answer 1

1

You can try something like this:

Edit 1

Updated code based on @Bergi's comment

function print(b) {
    return (b);
}

function notify(a, b) {
    var args = arguments;
    var params = args[0].toString().split("(")[1].split(")")[0].split(",");
    if(params[2]){
    	console.log(eval(params[2])(b))
    }
}

(function () {
    var a = 10,
        b = 20;
    notify(function (a, b, print) {}, b);
})()

Sign up to request clarification or add additional context in comments.

9 Comments

It's good, but it's not what am I looking for ... could you do something more in the lines of the question?
@philippe I have updated my answer. Hope it helps
Rajesh, the number of variable arguments in in f, not foo. Please review the question
Let me iterate my understanding. You have a function foo which expects a function f as a parameter. This function f can have variable parameters, and if it has 3rd parameter, you want to invoke it, but inside foo. Right?
Exactly.... the code snipped I wrote explains it well.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.