We all do it
You might be thinking what’s wrong with that, but I hope you’re thinking I’m better than that, I’d do something like this:
Great. What do you do if the alert text is dynamic? I’ve seen all kinds of ugly messes that usually lead back to the first approach or result in timing and scoping issues.
This problem isn’t just limited to event handlers, ugly nested anonymous functions are frequently used in callbacks and iterators. This results in poorer performance and uglier, less maintainable and bug prone code.
The right frame of bind
Yes, I really did just make that joke, it’s been a long week. In all seriousness
Clean, reusable and easier to maintain. Better than inlining or manipulating variables at a higher scope.