Tag Archives: type safety

Why JavaScript scares the hell out of the Java developers?

Today there is a clear tendency to move more and more of the presentation logic on the web client side. Yeah, that means the web browser. So if you want to get a slick Web 2.0 interface for your cool website, then you need to bring in the horror named JavaScript. Well, it’s a horror if you’re a Java developer… So why do we fear it?

Fear of the unknown

Fear of the unknown

Fear of the unknown by zephirance

Oh yeah, we all fear the unknown, so after years of Java, the weird scripting language that has a similar name only by chance, fits well between our darkest fears. They say it’s object oriented, but I can’t use the class keyword and heck if I know how to declare a class. And then the var type freaks me out because I love my favourite concubine – static typing. And undefined? Jeez! What’s wrong with plain ol’ null?

OK, I admit I was part of the group of Java developers that were afraid of this strange JavaScript stuff and I din’t like myself very much for it. I had to do something to change this so I tried reading a few good books on it, especially those for beginners. Because you see, JavaScript is nothing like Java and you really need to start from the beginning.

I learnt there are no classes in JavaScript but only objects. For someone coming from the Java world, this is quite a different approach. Don’t define a class and then create instances of it, as many as you like. Instead you get to define your instance directly by adding properties (already initialised with values) and functions. And should you need to create another object of the same kind, just copy its prototype from the previous one. Hm, that’s something! It can get you into a lot of trouble of you misuse it, but handling it with love – brings joy to your boring life and a touch of craziness, of freedom.

// declare the object directly
var aLovelyObject = {

	name: "lovely",
	purpose: "make love not war",

	givePurpose: function(newPurpose) {
		this.purpose = newPurpose;

	rename: function(newName) {
		if (newName != undefined) {
			this.name = newName;

// use the instance we declared above
aLovelyObject.givePurpose("promote darkness");

Isn’t that just lovely? And if we need another object of the same kind (whatever that would be), we have a lot of options out there that basically copy the kind (which we should call prototype) to our new instance. Even change it afterwards. I personally like JQuery style:

var anotherLovelyObject = $.extend({}, aLovelyObject);

We can even add some more properties and functions to the new object:

var anotherLovelyObject = $.extend({}, aLovelyObject, {

	speak: function() {
		if (this.name == "lovely") {
			alert("I love you");
		} else if (this.name == "hateful") {
			alert("I hate you");
		} else {
			alert("I'm confused");

So our second lovely object can speak now, while our initial one cannot. Very smart!

To me there’s no reason to dislike JavaScript because if you get to know it well enough it will surprise you with it’s abilities, help you write less code and make a lot of sense through it.

How to get to know JavaScript better – tests

Of course you can start reading about it. Here is a bunch of books that can prove useful:

JavaScript: The Good Parts

Image by nyuhuhuu via Flickr

Well, if you really are on to it, the best way to explore it is through tests. You can write tests in JavaScript too, just like you do in Java, you can even use Continuous Integration. There is a fantastic book that explains very well how to start looking at the tests in JavaScript, what your options are and how to approach this. So try to pick up Test-Driven JavaScript Development by Christian Johansen.

Good luck!