0

If I pass function to a function with same function name and handler name, which one will get precedence ? and how to access each of those two inside function in case in need to do recursion as well as refer to the passed function. See below code.

var f1,f2;

(function f(f){
    return typeof f(f2); /*please check below comments for output of this line*/
  })(function(f1){ return 1; });

/* this will call the passed function,why not recursion will not happen here? */

3 Answers 3

3

The function parameter gets precedence over the function's own name. If you shadow or overwrite a variable, you can't access it (unless it's a shadowed global).

Solution is to use different names.

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

Comments

0

The recursion doesn't happen simply because the argument of the function get precendence than the function itself. here is an example that shows it:

(function f (f) {
   return f.name;
}) (function funcName () { }); // this will return funcName

if we change the name of the argument to f1, f will become the reference of the function itself

(function f (f1) {
   return f.name;
}) (function funcName () { }); // this will return f

Comments

0

I see that you use jquery. So I want to ask where do you have declared your functions? inside

    <script type="text/javascript">
     $(document).ready(function(){ 
        function f(){
            return 'this is local function inside anonymous function, so it's invisible for recursion in aside of current document ready'
        }
    });
//or here? 
    function f(){
    return        'this function is a window object property, and must be visible for recursion';
    }    
    </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.