As you can see here, I have passed the named function, replaceLogic(), into the replace() method. The logic of the replaceLogic() function is such that it replaces incoming vowels with the "*" character. Once the replace method has executed, I am logging both the resultant string and the named-callback reference to the console. Here is the console output that I get:
replaceLogic is not defined
I had no idea that it was valid to use named functions in this context, or in the self-executing function block context; however, I kind of like it. I don't know if I can see the best use-cases for it just yet but, it never hurts to have a few more tools in the toolbox.
NOTE: Regarding my intro, I think self-executing functions might create closures as well.
I too like using named functions over anonymous functions. But i remember reading about some problems with it, just can't remember what exactly.
A quick google search found this IE bug, which may be what I am thinking of:
Just a list of things that don't make sense.
I found this article best for understanding named function http://yura.thinkweb2.com/named-function-expressions/
Wow - that's a beasty article. I read about half of it (more than my brain can handle tonight). I never really though of a difference between function declarations and function expressions (and how they differ in behavior). Thanks for pointing this out.
I've got this scenario mentally classified as "an anonymous function which has a name" (not that my definition is good, but I'm not sure if there is a good name out there for this type of function declaration).
In this scenario I believe that you can only use the name inside of the function itself. This is a definitive distinction when compared to "normal" named functions.
I haven't found any great use cases for this yet. For recursion I'm thinking argument.callee is gonna cover the use case.
Regardless - my brain now hurts just a little. Thanks for that!
I had never really thought to use this technique for recursion as Ben pointed out, and there are plenty of other ways to accomplish recursion without needing to use named expressions. Still, it is interesting to consider.
The main use case I had for experimenting with them was in debugging and building a logger object. Having named functions and expressions produces much nicer stack traces, and the logger can actually output the name of the function it was in.
But in the end, I've resigned myself to not using this technique, as it isn't fully cross-browser compatible.
Yeah, it's definitely an interesting thing, right? After reading the article that Nick posted, I think what's going on is that the function here, although it has a name, is a function "expression", not a function "declaration". As such, it is only available in that expression (unlike a function declaration which is actually pre-"compiled" so to speak). Very interesting stuff!
As far as using arguments.callee, the only "concern" there is that it is a deprecated approach. What does that mean? Probably nothing.
Yeah, from what Ben Alman was saying in my other post, this is not a fully cross-browser compatible feature. Although, Alman did seem to imply that the issue was mostly with one particular mobile device.