JavaScript is the new black …
Tuesday, April 3rd, 2007JavaScript is a very powerful language and the more I use it the more I see it’s power. It’s very LISP like with its use of associative arrays to manage objects and it’s use of functions as first class objects.
In JavaScript you can create a new function and associate that function with a variable. When a function is invoked with ‘new’ then the result is an object. For example:
// assign a function to a variable.
var func = function(x,y) {return x + y;}
// create an object
var point = new Point(x,y);
An object can have functions added to it at runtime. We call these functions methods since they are associated with an instance and have access to the variable ‘this’.
The function object has access to the arguments to the function as an array. So you could iterate through the variable list of arguments and take an appropriate action. For example, you could convert each parameter into a method call on the object and call it. Very powerful.
For a quick and cheap reference to what JavaScript can do I recommend the book JavaScript Pocket Reference by David Flanagan (2nd Edition) from O’Reilly.