Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-23-2013, 06:27 AM   PM User | #31
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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.


i'm not saying there's anything wrong with the first code, it does what it's supposed to. I prefer a different style of function declaration. I think it results from differences in our individual programming approaches. Nothing wrong with that. You can use JS to write LISP or you can use it to write C, or even javascript...


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.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%

Last edited by rnd me; 02-23-2013 at 06:31 AM..
rnd me is online now   Reply With Quote
Reply

Bookmarks

Tags
javascript, syntax

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:06 PM.


Advertisement
Log in to turn off these ads.