Geek Speak — HabitableZone

Geek Speak » in reply to re: You think you have it bad?

In defense of JavaScript

I have to take issue with

Javascript tends to lean towards 'ugly hackishness' naturally


No, languages don't lean, though they may have characteristics that encourage programmers to lean. I describe JavaScript the way people describe the game of Go: Easy to learn, but takes a lifetime to master. Which means that it's possible for amateurs to jump in and do stuff, with little real programming skill. Most of the JavaScript in the world wasn't created by programmers, but by Web designers who learned just enough about JS to accomplish simple tricks like "rollover" buttons and launching popup windows.

That's true of my employer--my JavaScript Department was only established this spring to get a handle on the problem of proliferating ad-hoc JavaScript that was written piecemeal over the years by whoever felt a need for it. Our big long-term project is called JavaScript Rearchitecture, and we're going to formalize and professionalize the use of JS everywhere on that huge site.

It may well be the only Web site in the world in which JavaScript is included in the source code control sytem, QA, and release processes, right alongside Java and C++.

In terms of the language itself, there's nothing about it that inherently discourages formalism and good programming practices. JS is very object-oriented, which means that it supports the discipline of inheritance and multiple inheritance. Many people would be shocked to hear that JS is object-oriented, because that side of the language hasn't been promoted much, and because amateurs have a hard enough time dealing with simple procedural scripts. Many people are also surprised to hear that JS has structured exception handling identical to C++/Java try...catch blocks. (I should say programmers are surprised...civilians just cross their eyes and grunt "uhh?".)

Part of the reason JavaScript doesn't get the respect it deserves is that it's interpreted, and traditionally programmers have looked down on interpreters as inferior to compiled languages. But JS has some novel strengths because it's interpreted. You can do cool things like extending classes at runtime (including the language's own built-in classes), by simply plugging-in new member functions and properties. You can do that either with an existing instance of an object, or--get this--you can retrofit all instances of a class with new members by accessing a class's "prototype" structure.

(This is so cool I'll give an example:
String.prototype.toDialect = function(...){...}

gives every string (even those that already exist) a new toDialect() function that (hypothetically) translates it to a dialect.

var Txt = "Indubitably";
Txt.toDialect("valley-girl")
even
alert( "Indubitably".toDialect("valley-girl") );
pops up an alert that says "fer sure fer sure". And BTW, notice the consequences of loosely-typing variables--JS just knows that a string--any string, even literals--is an instance of the String class.)

There's an eval() function that executes an arbitrary JS string, which lets you do things like create event handlers that can do anything at all because the script doesn't have to be prewritten at design time.

Did I ever mention that part of my job description is "JavaScript Evangelist"? Thanks for the practice!

Log in or register to post.

The whole thread (16 posts)