The two ways one writes JavaScript code..

I think sometimes in the past I have been guilty in my presentation on JavaScript (JS) of not defining for myself who my audience is. Actually I know that I’ve done a poor job of this. You see there are 2 ways to write JavaScript code. Something that I knew, but recently realized the importance of.

OO Baby
If you are building your own controls then you are all about building things in an OO manner because it helps you in the areas on manageability. You may or may not be testing your components with automated/unit test tools and quality is everything.. as well as browser compatibility. When doing this one of the major struggles in your world will probably be in the area of memory leaks (I’ve joined the coalition to kill IE6).

To be honest this is probably not you, but if you’ve come to one of my deep JS talks… this is my target (and my cardinal sin… sorry)

The way you probably use JS
Ok, the most common use of JS and the way that I think most people use it is to do minimal coding to wire up some validation or to wire up some interaction or even to include some Web 2.0 control (aka “Ajax Control”). Your mantra is “I want to get in and out quickly so that I don’t have to touch JS more than I need to…" We usually don’t take the time to test this stuff because it feels more declarative and that seems trustworthy (although you could test it)

This is exactly why jQuery is so popular. Not only does encourage this type of thinking (and even embraces it), but the re-usable plugins take on this same sort of thinking (the show/hide methods come to mind). That and the sheer fluency of the library makes it easy to use. The selectors are really a variation of the same selectors that CSS uses so you are re-using knowledge that should already be in your head as a web-developer/designer..

Anyway, while JS OO is cool.. most of us work the other way (and even those of us who use JS OO do most of our JS coding using the latter method)

Print | posted on Tuesday, April 21, 2009 11:44 AM


# re: The two ways one writes JavaScript code..

left by Kyle Baley at 4/22/2009 9:10 AM Gravatar
Do you have a favorite library for facilitating the former method?

# re: The two ways one writes JavaScript code..

left by Jay Kimble at 4/22/2009 9:38 AM Gravatar

I tend to prefer Script# using the MS ASP.NET Ajax library. Everyone has their favorites though... I know prototype is extremely popular.

I guess I like the comfort of coding in C# that Script# gives me (it's really a thin layer over JS)..

One of my past side attempts was to try to get Script# to produce a DLL that is testable in any of the _Unit testrunners (I wasn't successful, but I still think it's doable).. I got pretty far though... just couldn't get the proper core dlls loaded..


PS. I should really write about this.. someday..

# re: The two ways one writes JavaScript code..

left by The_witt at 4/29/2009 1:48 PM Gravatar
Now, I prefer to jump right in and work with the action script and build my own libraries for the smaller jobs. This allows for a greater movability across to the (UM...better) Mac side of things. For the bigger projects I agree w Jay with the AJX supplements; you can find a good reference at this site:

Good Luck!
Comments have been closed on this topic.