Okay, I'll admit it, this is an obscure one:
I came up with a use case where I wanted to create a function that had a real name (e.g., shows in debuggers and such) that was determined at runtime. And as far as I can tell, the only way to do that is to use eval.
"eval!??!?!" I hear you gasp.
Yes, eval. eval is evil when you can avoid it (which is almost always), but with strings you're in total control of, in a scope you control, with an understanding of the costs (you're firing up a JavaScript parser), to do something you cannot do otherwise (as in this case), it's fine provided you really need to do that thing.
So having dispensed with the eval is evil meme, here's how we do it:
var name = /* ...come up with the name... */;
var f = eval(
"(function() {\n" +
" function " + name + "() {\n" +
" console.log('Hi');\n" +
" }\n" +
" return " + name + ";\n" +
"})();"
);Pretty straight-forward. That creates a function with the name we come up with at runtime without leaking the name into the containing scope (and without triggering IE's flawed handling of named function expressions), assigning the function's reference to f. (And it formats the code nicely so single-stepping through it in a debugger is easy.)
Happy coding!