Javascript is not widely understood.

posted by pete on June 13th, 2007

The spectacular Dan Grigsby is visiting Toronto from Minneapolis/St. Paul this week. Dan is one of those mavericks that really sees beauty in code, and I learn tons from him by osmosis.

I was giving him a quick overview of how the Prototype library “extends” objects to include new functionality. It’s simultaineously an elegant hack, and boils down to the basic idea that complex objects are nothing more than name: value pairs, where the values happen to be anonymous functions.

The typical best practice when Javascripting is to use the Object.attribute or Object.method() syntax:


location.hash = 'bob'
location.hash #=> bob

window.alert('jim')

Dan took one look at the syntax and speculated that one could possibly address attributes or methods by calling them as if they were a named array, like so:


location['hash'] = 'alex'
location['hash] #=> alex
window['alert']('jim')

It makes complete sense, now. It’s perhaps a more explicit, “pure” syntax in the CompSci sense. I love that the lookup doesn’t differentiate between say, a String and a Function(). It still requires at least an empty () if it is a function, too. I’ve never seen this syntax referred to, but perhaps I wasn’t looking.

From a practical standpoint, I still prefer my Ruby-like syntax, but I feel like this little gem of insight maybe leveled me up, if you will. Perhaps you will enjoy a similar Aha! moment.

1 Response to “Javascript is not widely understood.

  1. Alexwebmaster Says:
    March 3rd, 2009 at 05:55 AM

    Hello webmaster I would like to share with you a link to your site write me here preonrelt@mail.ru

Leave a Reply