Today I Learned

hashrocket 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() {}
> a.name
undefined

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() {}
> a.name
'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
See More #javascript TILs
Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.