Classless objects and other chimera

Posted:

Recently, I had to wade deeper into the murky, fetid waters of JavaScript. Like an encounter with a cheap hooker, a session coding with JavaScript makes me want to shower and weep. What I had to struggle with on this occasion was JavaScript’s notion of associative arrays, which are implemented as Objects. All you Perler, Pythons and Rubyists out there, hold on! It’s not that associative arrays are a type of object in JavaScript, it’s that Objects are associative arrays (have I BLOWN your MIND yet, Java?).

Normally languages which provide associative arrays also build in routines to list the keys, the values or both from an instance of these variables. Not JavaScript. It’s true that you can iterate through an Object’s attributes with a for/in loop, but that’s a bit like using a hammer to repair a watch, which is an analogy that also holds for parsing query strings in URLs with JS. What a nightmare. It’s always 1996 in JavaScript’s world.

Weirded out yet? Wait! I’ve held the most offensive bit of JavaScript classes for last. In nearly every other God-fearing lanaguage I’ve used, Objects are declared. That is, the object’s attributes and methods are defined (usually) before an instance of that class can be used. So JavaScript, whose name clings to the fame of that paragon of OO-design Java, should have some kind of class declaration, right? I mean, declaring a class would make inheritence and object inspection easier and thus leverage the the major benefit of Object Oriented (OO) programming, right? Therefore, JS must have class declarations. But sadly, this isn’t the case. JavaScript sports Classless objects. Objects in JS are defined at run time procedurally, in what seems to be an attempt to make the brains of OO zealots melt.

Classes without an inheritence mechanism are like pants without bottoms and only David Lee Roth could get away with wearing assless pants.

How does JavaScript allow users to define their own classes? You make a generic Object and start assigning methods and attributes to it, as if it were a dictionary, which it really is! How does object inheritence work? Not very well, but you can try assigning to the pseudo-attribute .prototype. Or not. Apparently, prototype is sort of busted.

Another consequence of not having classes is that you can never find out what kind of Object you’re dealing. That is, you can’t ask an object, “what class do you belong to, little fella?” The confused bastard will answer “I’m an Object Object,” which sounds a little desperate — like the JS object really wants you to believe it’s a first class object, which it isn’t. This reminds me of a common folklore tenet in which all things have a Truename that, if uttered, will give the speaker power over that thing. Are JS objects superstitious? It’s true you can “override” (or overwrite) the .toString method with something about the class name, but that’s hackiferic.

Anyway, why bother with class heirarchies? Most JS scripts last only a short while and have so limited a scope. Then again, why bother pretending to have objects at all? Let’s call a hash “a hash” and be done with it.

Classless classes are the assless pants of the Internet.