Javascript get reference to current function

Sorry for the really weird title, but here’s what I’m trying to do:

var f1 = function (param1, param2) {

    // Is there a way to get an object that is ‘f1’
    // (the current function)?

};

As you can see, I would like to access the current function from within an anonymous function.

Is this possible?

gustavohenke

40k13 gold badges117 silver badges123 bronze badges

asked Jan 11, 2011 at 5:07

Nathan OsmanNathan Osman

68.8k70 gold badges250 silver badges350 bronze badges

Name it.

var f1 = function fOne() {
    console.log(fOne); //fOne is reference to this function
}
console.log(fOne); //undefined - this is good, fOne does not pollute global context

answered Aug 3, 2015 at 20:46

6

Yes – arguments.callee is the current function.

NOTE: This is deprecated in ECMAScript 5, and may cause a performance hit for tail-call recursion and the like. However, it does work in most major browsers.

In your case, f1 will also work.

Rory O'Kane

27.7k11 gold badges92 silver badges128 bronze badges

answered Jan 11, 2011 at 5:10

Christian MannChristian Mann

7,7605 gold badges39 silver badges51 bronze badges

7

You can access it with f1 since the function will have been assigned to the variable f1 before it is called:

var f1 = function () {
    f1(); // Is valid
};

f1(); // The function is called at a later stage

answered Jan 11, 2011 at 5:13

David TangDavid Tang

90.4k29 gold badges165 silver badges149 bronze badges

5

answered Aug 8 at 13:25

Not the answer you're looking for? Browse other questions tagged javascript function or ask your own question.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a function and the task is to get the name of function that is currently running using JavaScript.

    • Approach 1: Using arguments.callee method: It refers to the currently executing function inside the function body of that function. In this method, we use arguments.callee to refer to the name of the function. So we define a new variable as arguments.callee.toString(). Then we use (variable_name).substr to get the function and then we display it as an alert.

      Syntax:

      arguments.callee.toString()

      You may have to parse the name though, as it will probably include some extra junk.

      Example 1: This example display currently running function using arguments.callee method.

      <!DOCTYPE HTML>

      <html> 

      <head> 

          <title> 

              How to get currently running

              function name using JavaScript ?

          </title> 

      </head> 

      <body style = "text-align:center;"

          <h2 style = "color:green;"

              GeeksForGeeks 

          </h2> 

          <script> 

              function GFGs() {

                  var me = arguments.callee.toString();

                  me = me.substr('function '.length);     

                  me = me.substr(0, me.indexOf('('));     

                  alert(me);

              }

              GFGs();         

          </script> 

      </body> 

      </html>

      Output:

      Javascript get reference to current function

    • Approach 2: With the help of console.log method, we define a new function which returns the name of the function and call it the present function.

      Syntax:

      function getFuncName() {
          return getFuncName.caller.name
      }
      function teddy() { 
          console.log(getFuncName())
      }
      teddy()

      Example 2: This example display currently running function using console.log method.

      <!DOCTYPE html>

      <html> 

      <head>

          <title> 

              How to get currently running

              function name using JavaScript ?

          </title> 

      </head> 

      <body style = "text-align:center;"

          <h2 style = "color:green;"

              GeeksForGeeks 

          </h2> 

          <script> 

              function getFuncName() {

                  return getFuncName.caller.name

              }

              function teddy() { 

                  console.log(getFuncName())

              }

              teddy()     

          </script> 

      </body> 

      </html>

      Output:

      Javascript get reference to current function


    How do you call a function within a function in JavaScript?

    Nested functions in JavaScript..
    Write one function inside another function..
    Make a call to the inner function in the return statement of the outer function..
    Call it fun(a)(b) where a is parameter to outer and b is to the inner function..
    Finally return the combined output from the nested function..

    How do you check if a function is executed in JavaScript?

    how to check if function(s) have been executed?.
    You can drop the == true since they both return booleans. ... .
    If this condition if (function1() && function2() ){ is true, it means that these functions was executed and returned true..

    How do you pass a function in JavaScript?

    If you want to pass a function, just reference it by name without the parentheses:.
    function foo(x) { alert(x); } function bar(func) { func("Hello World!" ... .
    function foo(x) { alert(x); } function bar(func) { func(); } //alerts "Hello World!" (.

    How do you determine whether a function exists by using the type of operator?

    Alternatively, we can use the typeof operator. This operator will check whether the name of the declared function exists and whether it is a function and not some other type of object or primitive. In the example above, we test if nameOfFunction exists, and if it does we run it.