Geeks With Blogs
Liam McLennan hackingon.net

Chris Nicola left this excellent comment on Justin Etheredge’s blog:

Even with a competitive way to do SSJS [(server-side JavaScript)] on the Windows platform, I just have to ask... why would anyone?

Server-side JavaScript means that the server portion of a web application is written in JavaScript. Personally, I believe that server-side JavaScript will be the next big advance in web development.

If I were Microsoft I would be looking to hit a touchdown out of the ball park with server-side JavaScript, to win back the respect of the more advanced web development community.

Here are some of the reasons why server-side JavaScript will win.

Homogenous Programming Experience

With server-side JavaScript (SSJS) you can use the same language on the server, on the client and over the wire (JSON). JavaScript is even deeply integrated into a number of database platforms. This lowers the concept count for web development and reduces the need for context switching.

JavaScript Runtime Engines

There is a large and ever expanding list of quality, cross-platform JavaScript engines. Every browser contains a JS runtime and there is currently a gold rush on JS performance improvements.

Performance

If you have ever seen IE6 I know you will have your doubts, but JavaScript is fast. Not fast relative to C, but fast relative to languages with comparable features, and it is getting faster.

Interpreted Language

You are too old for training wheels, and programming is too old for a compilation step. Now that we know the benefits of agile, and TDD, and rapid feedback loops, an interpreted language is a big advantage.

Easy Metaprogramming

Dang!! The JS array type doesn’t have a filter/where method. Let’s add one:

// predicate is a function that accepts one parameter and returns a boolean.
Array.prototype.where = function(predicate) {
    var derivedArray = [];
    for (i = 0; i < this.length; i += 1) {
        if (predicate(this[i])) {
            derivedArray.push(this[i]);
        }
    }
    return derivedArray;
};

Now I can filter to my hearts content:

var numbers = [1,2,3,4,5,6];

var evens = numbers.where(function (number) {
  return number % 2 === 0;
});

// evens = [2,4,6]

or, I can modify an object on the fly.

var person = { 
  name: 'Dave' 
};

// add a method to an existing object
person.isAnonymous = function() { 
  return true;
}

Just like Ruby I can overwrite object properties at will, so mocking frameworks are not necessary:

// overwrite methods for testing
person.isAnonymous = function() {
  return false;
};

Easy Reflection

If metaprogramming is easy then reflection had better be easy too. The JavaScript for loop can enumerate the names of the properties on an object:

for (var key in person) {
 write(key);
}

// output is 'name'  'isAnonymous'

It is also easy to invoke functions on an object:

for (var key in person) {
  if (typeof(person[key]) === 'function') {
     person[key]();
  }
}

I used this technique for my JavaScript Object Builder.

Object Literals

As demonstrated in some of the previous examples JavaScript has a powerful object literal notation:

var movie = {
  actors = [Bob, Jill],
  budget = {
    currency: 'AUD',
    amount: 3000000
  },
  name: 'The White Whale' 
};

JavaScript as a Virtual Machine

I already linked to some information about JavaScript as a Ruby Virtual Machine. There is also scope for JavaScript to host other, prettier languages such as CoffeeScript.

CommonJS

JavaScript has traditionally been confined to the browser and supported by an extremely limited library. To address this issue in a standard way the CommonJS specification was created to define a common set of JavaScript libraries.

The State of Server-Side JavaScript

The language, libraries and the runtime engines are ready to go, but the web development frameworks are currently fragmented and incomplete. There are several which I will be keeping an eye on. Hopefully, within the next 12 months it will be practical to chose JavaScript as your server-side language.

If you are interested in learning the basics of JavaScript try my JavaScript Koans.

Posted on Monday, October 18, 2010 4:42 PM | Back to top

Copyright © Liam McLennan | Powered by: GeeksWithBlogs.net