Anonymous function expressions in JS bug me as they make stack traces and Chrome Dev Tools profiler output harder to use, so I've decided to name ALL my function expressions from here on in. The obvious thing seems to be to name them the same as the name I am assigning them to, e.g.

this.myFunc = function myFunc() {};

If I understand correctly, this means the second myFunc is the one that is bound within the function body, leaving it unable to easily access the first myFunc from the outer scope but...

I can't see how this is of consequence as I can't imagine a scenario where I would ever need to refer to the outer myFunc anyway. That said, myForesight !== theFuture and Javascript is a strange and dangerously liberal language, so... if this strikes any of you as anti-pattern, please speak up now! Thanks.

  • 1
    When you type function name () {...} the function is not anonymous. Either you do let fun = (function () { ... }) or function fun () { ... }. I don't see the point of mixing both. See also stackoverflow.com/questions/336859/…
    – coredump
    Commented Oct 15, 2016 at 13:23
  • 1
    Thanks @coredump, you're quite right it's not a problem when assigning to an explicit name but it IS a problem when a function is assigned to a property. I have updated the post accordingly. Commented Dec 12, 2016 at 17:55

1 Answer 1


I am using this in an event delegation framework I wrote. The "name" of the function determined which DOM event it responds to:

Foo.prototype.addComment = function submit(event, element, params) { ... };

The function itself doesn't need to know its own name, but the Front Controller in my library uses this name as meta data to decide whether or not to execute the function.

As for whether or not this is a good idea remains to be seen. It's not an often-used strategy, but there are edge cases where it can work.

  • It's a good idea if it works. Commented Nov 16, 2016 at 15:22

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.