Calling a function passed to a JQuery handler

Viewed 54

function test() {
  var str = 'adarsh';

  // f1(); - This gives an error.
  $('body').click(function f1() {
    console.log(str);
  });
}

test();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

I want to know why the above snippet gives an error when I try to access f1() inside the function test.

What is the function f1 scoped to?
I know it is not the window because I cannot access window.f1 after executing the above snippet.

Note: I know I can declare function f1 first and then pass the reference to it in the click function. However I want to know what is the point of naming 'anonymous' functions in such contexts if we cannot access them anywhere through that name.

4 Answers
Related