66

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?

3 Answers 3

63

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
6
  • 4
    I wonder who downvoted this. This is basically how one accesses current function in anonymous function. And the name is preserved even if it is then assigned to another object, so recursion is still possible.
    – Markos
    Commented Oct 15, 2015 at 6:34
  • 6
    @Markos—a function expression with a name is not "anonymous" any more. ;-)
    – RobG
    Commented Feb 23, 2016 at 0:42
  • 1
    @RobG It's just terminology. Since the name on function expression serves only for the code within the function, it is basically anonymous from the outside point of view. The only place where name is visible from the outside is function statement.
    – Markos
    Commented Feb 23, 2016 at 11:17
  • 1
    I wasn't sure when I first started learning JavaScript, but know I know that this is the correct answer.
    – wizzwizz4
    Commented Jul 5, 2016 at 16:08
  • 1
    This answer assumes that you know which function you are in - in which case it is indeed trivial. However, usually the requirement is to find out which function you are in at runtime, for debugging, stack tracing, etc. purposes, and this naming approach is useless.
    – Mr. TA
    Commented Jun 3, 2017 at 20:45
44

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.

7
  • 7
    arguments.callee is not the right tool for this situation. It is deprecated in ECMAScript 5 "strict mode", and prevents certain optimisations by minifiers.
    – David Tang
    Commented Jan 11, 2011 at 5:21
  • 24
    @Box9 is there an alternative?
    – Dirk Boer
    Commented Nov 18, 2014 at 9:04
  • 6
    @DavidTang it may be deprecated, but it is the only option available. Perhaps they should un-deprecate it.
    – Mr. TA
    Commented Jun 3, 2017 at 20:42
  • 3
    Do note that this does not work when using the 'strict mode'.
    – Alex
    Commented Aug 16, 2017 at 20:45
  • 6
    arguments.callee is very useful when you're doing a lot of cut and paste work across a lot of functions where naming the function would require significant manual updating. It's also safer if you ever refactor code as the risk of you now referencing the wrong function is very much reduced.
    – jmc
    Commented Jan 21, 2018 at 9:04
14

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
5
  • 1
    ...which is weird, but useful. Not quite an appropriate answer to the (ambiguously exampled) question, though, as George wanted to recurse in an anonymous function. Commented Jan 11, 2011 at 5:21
  • 3
    @Christian, it is not weird at all. Function declaration and function execution are two entirely different things. And I believe this is the more correct solution to the OP's question - var f1 = function () {} is an anonymous function. function f1() {} is not. Please see stackoverflow.com/questions/103598/… for why arguments.callee should be avoided.
    – David Tang
    Commented Jan 11, 2011 at 5:24
  • 1
    Ah, I see. Weird because the Function object was not fully constructed at the time of definition, therefore (in my mind) not assigned to f1. I thought that George was wanting to recurse in an anonymous function, say, in a JQuery event handler or something. Commented Jan 11, 2011 at 5:27
  • @ChristianMann it makes sense as the reference is used just when the function is called, not defined. this will work too: var x = function() {console.log(y);}; var y = 'test';
    – amik
    Commented Aug 3, 2015 at 20:16
  • Those will work but for a different reason – when the function will be called has nothing to do with it. JavaScript has a lexical scope, so all the references that are closed over, will be solidified at the definition site! The real reason why it works is that the variable definitions are function-wide, defining stuff later is just a syntactic sugar. So the referees exist already when the closure is created.
    – GolDDranks
    Commented Sep 5, 2017 at 12:43

Your Answer

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

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