For all of the issues with maintainability and the often-derided
inelegance of the DOM, JavaScript is actually a very interesting
and language. In addition to basic datatypes, it has a multitude of
features which one would expect these days and a few surprises.
Those hoping for features like first-class functions, closures and
dynamic typing will not be disappointed. One of JavaScript’s
function declaration mechanisms borrows heavily from Scheme’s
define. The following two definitions of the addFive functions
are equivalent:
function addFive(num) {
return num + 5;
};
var addFive = function(num) {
return num + 5;
};
Furthermore, newcomers to the language might be quite pleased (or confused) to discover that though JavaScript is object-oriented, it features a prototypical object system (picked up from Self) and friends) as opposed to one which is class-based. For someone who has ever been frustrated by writing classes which only get instantiated once, the ability to create one object, clone it and modify only select characteristics should seem rather attractive. In fact, JavaScript features a rather elegant syntax for composing objects definitions as they may be written using syntax identical to JavaScript’s associative arrays. Take the following example:
var panda = {
bamboo: 5,
tree: "Look at all the pretty lights!",
curry: function(noodle) { return noodle + 5; }
};
As is evident, there is an object called panda which has three
attributes, bamboo, tree, curry which are of type number, string
and function respectively. This could actually be done differently
by creating a new object and adding things dynamically.
var panda = new Object();
panda.bamboo = 5;
panda.tree = "Look at all the pretty lights!";
panda.curry = function(noodle) { return noodle + 5; };
It should be clear that functions which are incorporated into object this way automagically become methods. However, the above examples are not the only ways to create an object and do not explicitly showcase prototype functionality as we are creating an object “from scratch”. Let’s show off another example in which we use a function as an object constructor and then extend the prototype to add a function.
function Account(chk, sav) {
this.checking = chk;
this.savings = sav;
}
Account.prototype = {
total: function() { return this.checking + this.savings; }
};
Here, we create a function which acts as a constructor method (note
the use of the this keyword) for an object we’re going to call an
Account. Then, we extend it’s prototype to add a function.
Calling the Account() method with the new keyword will give us
a new object. Then we can use it like this:
var acc = new Account(42,613);
var myMoney = acc.total();
The variable myMoney will have the value of 655 after this. More
can be added to Account by extending it’s prototype. Using a
slight variation in syntax, let’s try adding a way to withdraw
everything from the account.
Account.prototype.withdrawAll = function() {
var amount = this.total();
this.checking = 0;
this.savings = 0;
return amount;
}
Now, we continue with our example:
acc.withdrawAll();
var newTotal = acc.total();
Clearly, newTotal now has a value of zero because we’ve taken
everything from the account! See? JavaScript actually some useful
and novel features in it. This is not to say that it is not without
problems. Looking at the feature list of any JavaScript toolkit
will give some idea of what the language lacks. My personal
favorite is jQuery. Still, I believe that much
(not all) of the dislike for JavaScript stems from problems related
to browsers. Of course people get annoyed when the DOM is
inflexible and different JavaScript engines behave differently in
different browsers! The JavaScript ecosystem is rife with
idiosyncrasies and inconsistencies. On the other hand, the language
itself is quite powerful! Projects like
SproutCore seek to leverage JavaScript
to create exciting new ways to develop applications that just
happen to live in the browser. Evidence of this can be found in the
move to standardize the language through
ECMAscript
and it’s subsequent inclusion in various projects and frameworks,
many of these are not explicitly tied to the web. Yes there are
things like Flash and
friends but it is also being used in forms like
QtScript and
Palm’s new webOS. Look,
there are so many reasons not to like things in the wide world of
computing and JavaScript is often a favorite target. So, before
judging because you don’t like the DOM or because of some feature
which you feel is missing (you may very well be right), I encourage
you to sit down with a JavaScript prompt
(Firebug runs in-browser but I also
like
spidermonkey-bin
on the command line) and experiment with what is actually a
fascinating language with much potential.