NeverSawUs

The Four Blind Men and the Rhino

Imagine, if you will, a large, parliment-esque room. In it, an audience — a group of interested folk who long to hear the findings of four blind wise men vis a vis the Rhinoceros.

Our actors:

  1. Professor Hubert J. Spidermonkey
  2. John Squirrelfish, Esquire
  3. Miss Charlotte Veeate the 3rd, Rhodes Scholar
  4. Jimmy I. Explorer, drooling idiot

These four men have been charged with the task of examining closely the wild Rhinoceros, and presenting their findings in a scholarly language called "ECMAScript". The audience is a fairly conservative set of folk, wherein lies the problem: Jimmy I. Explorer has a tendency to scream expletives whenever he observes something he does not understand. The other scholars have broad experience in the field of zoology — there's not much on a rhino that will confuse them.

Jimmy's sum total of knowledge about animals, however, comes from humbler origins — a speak and spell.

Much rides on their dissertation. If it goes poorly, the four men will be disbarred and cast out, and knowledge of the Rhinoceros will likely never be attained.


the miracle worker

Prior to the investigation, Professor Hubert J. Spidermonkey sagely suggested they hire a tutor for Jimmy. Their job is to take Jimmy into the field, and sign the appropriate words into Jimmy's hands while he touches something he doesn't understand until he stops screaming expletives and instead starts yelling the learned word. It adds about a month to the research time, but it's well worth it, by the end Jimmy has nearly the exact same vocabulary as the others.

The day of the dissertation rolls around, and Jimmy steps to the podium to deliver his portion of the dissertation. The words come slowly, and are occasionally slurred, but his findings are accurate, despite the inelegance with which they are delivered. All seems well, until...

Jimmy lets loose with a flurry of colorful language that could peel lacquer. Parents in the audience cover their children's ears, the elderly clutch their chest in the throes of expletive-induced heart attacks, and general chaos ensues.


The Aftermath

Ms. Veeate peers over the wreckage of the podium. A chandelier falls on a newly unoccupied pew. She turns slowly to Prof. Spidermonkey, and pantomimes a wide-eyed "What the hell, man?!" Jimmy is sleeping soundly on the floor, and the three encircle him, intent on revealing the source of the florid language. They wake Jimmy up with a gentle kick, and ask him.

Jimmy unravels the mystery slowly, but the gist is that another interpreter arrived after the one Prof. Spidermonkey hired, and undid the explanation of just one discrete piece of knowledge. Armed with this new, awful term, he singlehandedly ruined the dissertation.


The solution

John Squirrelfish posits the following solution: Instead of teaching Jimmy about the varying animal parts Jimmy might encounter, and opening themselves up to the possibility that some deviant may happen to undo that knowledge, they should instead wrap parts of the rhino in seran wrap, and teach jimmy what those pieces mean. The three intelligent scholars can easily deduce what lies under the seran wrap, while Jimmy will know by rote memorization what that piece means. And that evil tutor could not easily replace bits of Jimmy's domain knowledge of rhino parts without knowing that he'll actually encounter them wrapped in seran wrap.


The moral

If you hadn't guessed it yet, I'm talking about Javascript — and further, I'm talking about how to avoid making Internet Explorer choke on things it doesn't recognize.

The three wise men's first approach is very similar to the following:

if(Array.prototype.indexOf === undefined) {
    Array.prototype.indexOf = function(needle) {
        for(var i = 0, len = this.length; i < len; ++i) {
            if(this[i] === needle) {
                return i;
            }
        }
        return -1;
    };
}
[1,2,3].indexOf(2);

Which is great! Except for the fact that they're globally changing how Internet Explorer deals with indexOf. A better solution relies on all four of them agreeing to use one function instead of the default:

var indexOf = function (haystack, needle) {
    return haystack.indexOf(needle);
}; 
if(Array.prototype.indexOf === undefined) {
    indexOf = function(haystack, needle) {
        for(var i = 0, len = haystack.length; i < len; ++i) {
            if(haystack[i] === needle) {
                return i;
            }
        }
        return -1;
    }
}
indexOf([1,2,3], 2);

This way, no one is trampling on the global Array.prototype.indexOf.

The End. Thanks.