Today I Learned

A Hashrocket project

Name that anonymous function

I use anonymous functions in javascript a whole bunch but anonymous functions don’t get a name:

> a = function() {}

Its helpful to have a name for the function when you’re debugging and need to determine the caller of a function or if you want to determine which function has been passed in as an argument.

It’s easy to name anonymous functions:

> a = function myName() {}

But weirdly, javascript doesn’t know about a function named myName so you’re not polluting the namespace:

> myName()
ReferenceError: myName is not defined