Being in the Web 2.0 "business'' certainly provides me with a lot experience looking at and fiddling with javascript code. In all this writing and reading I've started to notice some trends in development of javascript. In particular I have noticed a tendency for people to write javascript code in a way that is paradigmatically similar to the languages with which they are more comfortable. So I've decided to (with tounge planted firmly in cheek) detail what Javascript is NOT:
All of these trends are fairly common and you can see each of them when reading code. Java is invoked often with the fear of using object literals. Very often you'll see code like.
var s = new String();
s = 'blah';
Whereas it's totally acceptable to skip the first and just set a variable equal to an object literal. This is also common with Arrays, and to a lesser degree with Objects.
The Ruby tendency has been cropping up a lot with the metaprogramming trend. A recent presentation has provided a metaprogramming example of implementing FormBehavior. The proposal also recommends that users "[not] fear eval()'' and "use with().'' Douglas Crockford discusses the issues inherit with both of these on his javascript site. Crockford, especially through his Y!DN video series, provides great introductions to a more Javascript way of thinking.
I have to admit: I'm a sucker for the Javascript is Python tendency. Javascript doesn't make this attitude particularly hard by providing a lot of the -isms I'm used to using in Python. Iteration of objects and arrays can be done very similarly to how one might do it in Python for example:
var nums = [1,2,3];
for (var a in nums) {
var num = nums[a];
print(num);
}
nums = [1, 2, 3]
for a in nums:
print a
So certainly some of the paradigms I'm used to in Python presist in Javascript; this same example would be done by using the each method of the Enumerable class in Ruby, and so would take a different approach. What's interesting is that where the each method could be a bit cumbersome to implement in Python, it's very straight forward in Javascript:
Array.prototype.each = function (func) {
for (var a in this) {
func(this[a]);
}
}
var list = [1,2,3];
list.each(function (a) { print(a); });
This not only highlights that javascript isn't quite Python, but it shows that it isn't really Ruby either, yet it simultaneously highlights the ability to do closures and anonymous functions which Ruby developers consider one of Ruby's greatest powers. This example actually shouldn't be used since it would mess up iteration of the object, but it demonstrates another of the great powers of JavaScript. Objects are not instances of classes. Objects are copies of other objects which supply a prototype of how an Object should be defined. In this way an instance of Array is a copy of Array which inherits the same prototype as the base Array object. This provides some really great implications for dynamicity. It's no wonder that the conventional dictionary was merged with the object in Javascript. It's particularly easy to make a new object in this way:
function obj () { this.list = [1,2,3]; }
obj.prototype = {show_list:function () { print (this.list); }};
var o1 = new obj();
var o2 = new obj();
Here I'm using a constructor, but it's actually possible to implement objects without using constructors which Crockford discusses in some of his advanced javascript lessons. Prototype-based object orientation, anonymous functions, and lexical scope, make javascript a really exciting language to work with that is not something one should try to meld into what they're used to working with but a language where the developer should embrace Javascript's power instead.