New in JavaScript 1.7

Over the last two years or so, JavaScript got a lot of attention due to the surge of client-side scripting. It turned out that a few tweaks to a previously rather dull-looking, but dynamic language made JavaScript much more attractive, adding (or promoting) concepts like object orientation, function pointers and extension of built-in types.

The success of the Mozilla project probably made a difference, since it provides free and standards-compliant implementations of JavaScript and DOM, making dynamic webapps much less of a hassle than they used to be. Recent builds include support for JavaScript 1.7, and the Mozilla developer center lists some quite interesting additions to the language. Most of them seem pretty familiar to Python or Ruby programmers, but it’s nice to have these features available on web browsers as well. Some of the features are:

  • Generator support: anything generating a list of objects is much nicer to implement using generators if you don’t want to return the whole result at once.
    function x() {
      var i = 0;
      while (true) {
        yield i++;
      }
    }

    yields any number of consecutive numbers that can be consumed via .next():

    var gen = x();
    for (var i = 0; i < 100; i++) {
      document.write(gen.next() + " ");
    }

    produces 100 consecutive numbers – without resorting to arrays for intermediate storage or callbacks.

  • Iterators provide a cleaner solution of the “for…in” construct that breaks when a library like prototype.js adds custom properties to built-in types.
  • Array comprehensions, a favourite Python construct of mine, have been added. They come in handy for generating arrays or projecting objects to arrays. For example, you could write
    var evens = [i for (i in range(0, 21)) if (i % 2 == 0)];

    where range(x, y) is a generator function. I guess it would be easy to provide a generator iterating over an array of objects, allowing to write something like

    var ids = [obj.ids for (obj in iterate(objects))];
  • More functional-style programming using let. Probably the biggest improvement for fans of functional programming, though I’m not exactly sure how useful this turns out to be.
  • Multiple-value returns reduces boilerplate code when a tuple instead of a single value is returned from a function. So it’s possible to write
    function x() {
      return [1,2]
    }
    
    [a, b] = x();

Of course, the real question is when and if the other browser vendors adopt JS 1.7 (or even 1.5) – until then, it’s probably most interesting for Firefox plugin developers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: