Aracılığıyla paylaş


JavaScript, the underappreciated language

I've spent a good part of my career writing compilers, editors, and runtime libraries, so I've formed some fairly strong opinions about what makes a good programming language. Which is why if you'd told me six months ago that I would be programming inJavaScript -- and liking it -- I would have thought you were crazy. But I've been doing a lot of coding in JavaScript lately, and I have to admit, it's actually a pretty well-designed language and pleasant to use.

To me, what makes JavaScript not just another poorly designed scripting language is lexical scoping, closures, and functions as first-class objects. You can assign a function to a variable:

var foo = function() {...};

And you can pass that function around like any other object. You can define functions within functions, and when you do that you can create closures that use the local variables of the outer function:

function outer() {
var foo = 5;
return function() {return foo;};
}

And if you evaluate that function, it will find the right foo, even if you call this function from a place where foo has a different definition -- it's lexically scoped!

function differentFoo() {
var foo = 7; //not the same foo!
var fn = outer();
fn();
}

(Okay, for the purists out there, there are some constructs that aren't lexical -- such as with statements -- but for the most part JavaScript code behaves as if it's lexical)

I also enjoy the traditional benefits of scripting -- no compiler, no type signatures (especially, remembering which subclass of EventArgs a particular event requires), optional parameters, etc.

It's not all roses, of course. You sometimes need to resort to eval for relatively simple things (like #include), which as a programming tools guy makes me cringe -- eval is Turing complete and blocks tools from understanding the code. And I've been bitten occasionally by setting foo.Property instead of foo.property, and accidentally creating a totally new property. (Although it happens much less often than I thought it would -- unlike many scripting languages, JavaScript detects undefined variables, just not undefined properties) And while in many ways I like the idea of prototype-based inheritance, it doesn't always work -- sometimes, you really need a base class constructor, and at that point you're inventing your own language mechanisms.

But, all things considered, I'm actually pretty impressed with the design, and I'm looking forward to seeing what the next version of EcmaScript brings.

Comments

  • Anonymous
    November 21, 2006
    The comment has been removed

  • Anonymous
    November 21, 2006
    I never understood JavaScript that well, but with all the AJAX & Web 2.0 hype, I again started to play with JavaScript and now I like it very much, because programming JS using the script.aculo.us library [which is based on the prototype library] is like a breeze.

  • Anonymous
    November 26, 2006
    I agree!!! JavaScript is worth researching, especially when you understand the importance of implementing cross-browser feature for client stript. I like it very much!

  • Anonymous
    September 23, 2007
    I want to link to Scott Bellware's post on attitudes regarding classic ASP. He makes an excellent

  • Anonymous
    September 23, 2007
    I want to link to Scott Bellware's post on attitudes regarding classic ASP. He makes an excellent