Thursday, October 4, 2007

Things I'd like to see in JavaScript

Recently, someone asked me what features I'd like to see added to JavaScript. In the week or so since then I've had some time to think about it. Here are some of my thoughts:

Code inclusion

Right now, when we want to dynamically includes some javascript on the fly (for instance to ensure that we only load the features that we need) we have to resort to document.writing a script tag. This is exceptionally inefficient. I would be nicer if we could explicitly include javascript files using something like: includeScript('/scripts/myScript.js'). It might be nice to allow a call back function to be passed along too.

Site persistent JavaScript

Currently, every time you unload a page, the compiled javascript is tossed out the door and new javascript is compiled when the next page loads. I don't know about you, but 80% of my code doesn't change page by page. It would be great if we had a way to "save" our compiled JavaScript and simply reuse it on each page. This way, I can load my libraries once and never have to worry about it again. It would also be necessary to identify some code that lasts only for a single session and some that lasts "forever" (same idea as with persistent cookies)

Native set objects

In the past I've run into confusion with some of the new Array methods that are being introduced by Mozilla. Specifically, it turns out that [].any(function (){return true;}) !== [].all(function (){return true;});.

When I chatted with some of the Mozilla guys about this they said that this has to do with set math. That got me to thinking about sets in general. There are some things that one can do with sets that are generally useful.

  • You can intersect, union, difference sets. Calculating the intersection of two sets (lets say sets of DOM Nodes) can be exceptionally useful.
  • Every member of a set is unique. Again this is a useful feature for working with collection of DOM Nodes.

There are other features of sets that I would like to be able to use. I could build a JavaScript implementation of a set but its MUCH slower than a native implementation would be. Since its generally useful, its worth adding to the language

Date Literals

Since strict JSON only allows literal notation, there is currently no way to include real dates in a JSON construct. Dates are one of the most useful objects we have in JavaScript and having a literal for it would be wonderful.

Those are my ideas. What do you think would be a good addition?


  1. Hi, I just found this post while looking for a Set library for Javascript (I need one in Actionscript but figured I could port whatever easily). Do you have a recommendation? All I have found is dojo.connections.Set. Any help would be much appreciated.

  2. Jotham,

    I've implemented part of a SET in the past but I've never published the code. The problem is that its a little kludgy.

    Here's the secret sauce: save the members of the set as the keys of an object literal

    If I get time I'll try to post my set code this week.