Blog


The Seedy Underbelly of JavaScript

I was watching my del.icio.us/popular links today and saw "JavaScript.com" scroll by. Amused, I decided to take a look. I remember looking at sites like this back in '98-'99 when I wanted to do "cool" effects, just copy-and-pasting snippets into a page and hoping that it would work.

So I started to look through the snippets on their web site. I knew that some bad JavaScript code was still written - but dear god - there's some awful code on their site. I can't even dignify it with a link, for fear that some PageRank may bleed their way.

I then stumbled across a so-called "Add/Remove an Event" snippet. Intrigued, I decided to take a look. You may remember that I won the Quirksmode addEvent contest last year with this entry. Honestly, it's pretty rough - I've moved on and use other techniques these days.

Now, I'd like you to take a look at my code and compare it to the code that was presented on JavaScript.com:

My Original addEvent/removeEvent Code

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

The "New" addEvent/removeEvent snippet from JavaScript.com

//------------------------------------
// addEvent
function addEvent(obj,type,fn){
    try{
        if(obj.addEventListener) obj.addEventListener(type,fn,false);
        else if(obj.attachEvent){
            obj["e"+type+fn]=fn
            obj[type+fn]=function(){ obj["e"+type+fn](window.event)}
            obj.attachEvent("on"+type,obj[type+fn]);
        }
    } catch(e){/*alert(e)*/}
}

//------------------------------------
// removeEvent
function removeEvent(obj,type,fn){
    if(obj.removeEventListener) obj.removeEventListener(type,fn,false);
    else if(obj.detachEvent){
        obj.detachEvent("on"+type,obj[type+fn])
        obj[type+fn]=null
        obj["e"+type+fn]=null;
    }
}

There's a couple points about this snippet that I find particularly amusing:

  1. It's presented under the copyright of some other blogger: askApache.com (although, this seems to be more of a case of mistaken identity than anything else).
  2. There's an extra try/catch block that silently garbles up any errors. What errors? You're binding an event!
  3. There's an extra check for IE (obj.detachEvent) but doesn't throw any error when the event isn't able to be bound.

In all, I'd hate to be the pour soul trying to debug my first JavaScript programming and not knowing why I wasn't getting any relevant errors. We should get Sun to start flexing their JavaScript trademark muscle and take down sketchy sites like this, passing them over to non-profit organizations who could actually move JavaScript out of the dark ages.

Update: The askApache webmaster has replied in the comments - it was a misunderstanding and the relevant copyright info is being put in place. Yay! That being said, JavaScript.com still has really poor-quality code. It's like we need a reset switch for the world of JavaScript knowledge - delete all the old, bad, code and stick with the current, high quality, code and libraries.

Tags: snippets, code, programming, javascript, tips, piracy

Modern Visual Programming

Today, the topic of Visual Programming happened to come up a couple times and I thought a discussion of the various (available) options would be interesting.

The one that I was immediately familiar with is Automator, which will be released with Tiger (OSX 10.4). Granted, this may not be a purely visual programming language, it does make the process of task automation painfully simple. My simply specifying a couple functions, matching up their input/output types (for example 'image', 'url', etc.) you can create a reusable script for your personal use. I'd imagine that something like this would play well with power-users/programmers who want to get the most of their data.

In anticipation for Automator (and Spotlight), I've begun the process of keeping my personal data in the Apple-sponsored applications (for example, Mail, iPhoto, etc.) and keeping a duplicate version always available online (Gmail, Flickr, Delicious, etc.) just in case. My hope is that by keeping all of my data centrally located, the process of task automation (even visually!) will become that much easier, but I digress.

Josh pointed out another visual programming language: RCX Code, the code behind Lego Mindstorms. Apparently there are two 'frontends' to developing for Mindstorms: 1) Targetted more at people who are not readily familiar with programming, a visual interface for developing an application (some example, and exciting, screenshots). 2) The raw code, obviously for the heavy-duty programmer.

What intrigues me about this particular interface is how Lego takes the complex processes of threading, synchronization, and looping (granted, not complex, but for a beginner, it could be daunting). Threading is represented by two columns of blocks, side-by-side, each one containing a process which needs to be completed. Once the code is compiled, the complex threading gets broken down and made such that even the basic controllers in the Mindstorms can handle it. Also, for looping, a large block is chosen, designed to repeat a number of times. Mini block processes are then dropped into the larger block, all of which will repeat those given number of times. I find this to be intriguing and I wonder how many young programmers are growing up with this as their foundation. As with the Apple computers and Logo of old, maybe these languages are leading the way for a whole new generation of developers.

Tags: automation, code, corp:apple, programming, rcx, task, visual

Current Projects

jQuery JavaScript Library

jQuery

Comprehensive DOM, Event, Animation, and Ajax JavaScript Library.

Recent Projects

Pro JavaScript Techniques

JavaScript Book

The best techniques for professional JavaScript. Published by Apress.


Hosting provided by the cool dudes at Engine Yard.