rob cherny.com

About

My name is Rob Cherny and I'm a professional Web Developer with 16 years of experience creating Web sites and Web-based applications. This site is where I write about my work, and random other things...

Learn More

Loading...

Close Tab

Web Development

JavaScript Growing Up

So ordinarily was thinking I'd keep this sort of thing for the Link farm over there in the right column (or here) but I couldn't help thinking like some of this deserved more attention. The other day I stumbled over "Namespace Your JavaScript" over at Dustin Diaz's Web Standards with Imagination. In it he's touching on things which have been getting more and more attention from hard core programmers as things like Ajax and libraries such as Prototype get more and more popular. And frankly, I think it's about time.

Dustin talks up an advanced JavaScript technique which features the ability to add both public and private methods and properties, a key feature in high end code. JavaScript gets a bad wrap. But like any code, there's good code and bad code, simple code and advanced code. All the cut and paste JavaScript out there has ruined its image for years. Things like this are professional and advanced, even dare I say object oriented approaches. There's been object oriented approaches for a while now, but this takes it to a whole new level. Who knew.

In his article he takes a creative approach I hadn't seen until recently, which is self calling functions. A while back over at Ajaxian was the first time I'd seen it, where they discussed a variable collision. The article Self Calling and Namespaced discussed the solution, which is one of many really, but I love the notion of the self calling function which can be used to set up a private space. I've spent a fair amount of energy looking at more object-based approaches to keeping things from the global scope. But this is pretty cool:

  1. (function()
  2. {
  3.     // insert isolated code here
  4.    //  and it's called right away!
  5. })();

However until now I'd been looking at Object Literals and other, older examples which defined all the methods and properties in the scope of an object. I've used similar techniques to some extent on some larger enterprise projects where I simply defined a global object space where all the other code lives to prevent collisions with any code the client might add or be included with. I also do it on smaller projects from time to time. It protects your code, which especially on larger systems is real important.

  1. var mySite = new Object();
  2.   mySite.test = function(){...}
  3.   mySite.demo = function()
  4.   {
  5.         mySite.test();
  6.   }

Another less complex way to do this is just to use prefixes on your functions -- for instance I could prepend "rob_" to all my functions and variables. But I prefer objects or, depending on what you define as an "object", an object-like approach.

There's been people doing this for a while now here and there, but only now is it getting more attention. Youngpup published (Google's Aaron Boodman) scripts like his Dom-Drag script way back in 2001, and it was set up as an Object Literal. But Aaron's a JavaScript freak (the good kind of course).

At any rate, I've long felt JavaScript deserved the second look that it seems to be getting now from serious programmers. If you're coding a high-end application UI with techniques such as Ajax, or integrating with enterprise systems, you best know what you're doing. Things like this will only improve its respectability. I've heard from real hard core programmers who have picked apart Sam Stephenson's Prototype library and called it a thing of absolute beauty. There's plenty of people really pushing the envelope these days. It's very exciting, especially if you have no life like me. ;-)

Just for equal time, I guess I should link to the counter-point on Object Oriented JavaScript.

At any rate, Dustin's article links to a guy who's clearly examined the possibilities, Douglas Crockford. He's got lots of great articles, a nice tutorial which is better than many I've seen, as well as the article Dustin links to and a great article, JavaScript, The World's Most Misunderstood Programming Language.

Here's hoping there's some new life for JavaScript and JavaScript as programming.

Apr 28, 01:17 AM in Web Development (filed under JavaScript)

commenting closed for this article

In This Section