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?

cybertextron
  • 10,547
  • 28
  • 104
  • 208
  • I don't get it. `f` is a function, which has *parameters*, whose number can be determined, but that cannot be "accessed"? – Bergi Nov 24 '15 at 05:54
  • Do you want to call `f(1, 2, retry)`? – Bergi Nov 24 '15 at 05:55
  • `f` is a function, which has parameters, whose number can be determined, but `how` can I access those **extra** parameters ? – cybertextron Nov 24 '15 at 05:55
  • I guess wrap them parameters that you want to pass into an object, and pass that object in the function. That way, inside the function then, you can check if a particular property exists (or has a certain value) and then do whatever you want to do with it. – Tahir Ahmed Nov 24 '15 at 05:56
  • @Bergi: The update is clear. `retry` is a function, but it may be present or not. If it is, then I want to call it ... if it is not, I want to call `k`. Make sense? – cybertextron Nov 24 '15 at 05:56
  • Similar/Related [Get function's default value?](http://stackoverflow.com/questions/32934617/get-functions-default-value) – Tushar Nov 24 '15 at 05:56
  • 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. – Bergi Nov 24 '15 at 05:57
  • @Bergi Could you better formulate your answer? – cybertextron Nov 24 '15 at 05:57
  • @Tushar It's not Similar / Related – cybertextron Nov 24 '15 at 05:58
  • 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`. – Bergi Nov 24 '15 at 05: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. – Bergi Nov 24 '15 at 06:03
  • @Bergi you got it ... – cybertextron Nov 24 '15 at 06: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? – Bergi Nov 24 '15 at 06:05
  • @Bergi if the third element is present, I want to invoke it, as shown in the question ... `retry();` – cybertextron Nov 24 '15 at 06:06
  • 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. – Bergi Nov 24 '15 at 06:07
  • @Bergi I understand what've you said. Sorry for the confusion. I want to get the string "retry" ... – cybertextron Nov 24 '15 at 06:09
  • OK, that "It can be a function" and "`retry()`" were really confusing if you wanted a string… Closed with the appropriate duplicate now :-) – Bergi Nov 24 '15 at 06:15
  • @Bergi I think I explained the problem well. I'm not an expert in `JavaScript`.I simply want to know know if `retry` is present in the `f`'s arguments, and if it is, how can I invoke (`retry()`) it. – cybertextron Nov 24 '15 at 06:21
  • 1
    If you're still trying to invoke it, then you didn't really understand what I said, sorry :-) Maybe you should ask a new question that **only** explains [the problem that you (think you) need this for](http://meta.stackexchange.com/q/66377) and not your attempted strategy to solve it. – Bergi Nov 24 '15 at 06:23

1 Answers1

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);
})()
Rajesh
  • 24,354
  • 5
  • 48
  • 79