Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 31 to 31 of 31
02-23-2013, 06:27 AM #31
- Join Date
- Jun 2007
- Thanked 569 Times in 550 Posts
first off, i didn't even look at the code, i was simply trying to show that function statements and expressions are almost identical in nature, and that expressions (especially those without local names) are not 'capable of a lot more' than statements. indeed, the code replaces those magical expressions with statements and seems to execute just fine. The code wasn't something i'd ever use, so i didn't give it my typical level of scrutiny, just enough scrutiny and firebug re-writing to show my function statement version in action. I don't care much for old IE, and I like to use inline (attrib-based) events for almost everything except grids, media events, and dnd handlers.
My original main point here: always name your functions. I can see that if you tend to code more expressions than statements it can seem repetitive to use local names, but it's a good practice for several reasons i've already outlined.
i will confess that i was fooled by the orig addEvent felgall posted. It looks like it sniffs the browser each time. That is doesn't is (to me) unexpected. I didn't pickup JS last night, so if something fools me, that speaks more the readability of the code than my literacy. Giving the anon functions names goes a long way toward readability. polymorhphism is fine for a compiler, but humans can only chunk so much, and that mental energy might be better spent on the logic, not the structure.
For the styles of programming i tend to use (data-centric functional, and event-driven), function statements plug-in nicely. A handy helper function can work much like Enums or constants instead of dot paths to make coding easier and cleaner. In conjunction with dependency injection, function statements encourage a readbility-enhancing flattening of the code, ameliorating the editor-crushing indentation problems that you see on a lot of async code using expressions and anon callbacks.
As I moved away from classes, OOP and even protos, i came to appreciate function statements more. I can see that if you do a lot of OOP you might prefer expressions, since that's typically how methods are bound without repeating yourself. Most of the code i write os not verp OOPish. Myself, I rarely use the "new " operator, and within modules, i tend to publish what i public-ally need to exports in an admittedly repetitive block at the bottom of the module code, just above where i paste in all the utility function statements that support the module code that appears first-thing. i have used some fancy routines to "publish all functions as methods", but usually, it's simply a matter of poking through the few levers i need while keeping the gears oiled and the belts out of sight.