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?
retryis not a function. It's just a variable name. There is also a functionretryin an outer scope, but it has nothing to do with theretryparameter of the anonymous function you are passing tofooasf.f" - not exactly. In your current code, you're not passing any arguments tofyet. There's a functionfthat may accept 2 or 3 arguments for its named parameters. Having that functionf, 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."retry"which is the name of the third parameter?