JavaScript Is NOT Java!

Versione italiana

When I tell that JavaScript has nothing to do with Java, people say that I am exaggerating. And maybe they think I am fighting some strange kind of holy war. But I am just trying to highlight a real problem. If you use Lisp, and someone convinces you that you are using Java… well, your programs simply can’t be decent. For 2 reasons: you are not using the features of Lisp, and you can not use the features of Java.

Here are some facts that people can hardly question:

  • Java has classes and interfaces, JavaScript has not. This is because Java is Object Oriented and JavaScript is not (altought it has an object type).
  • JavaScript has a function type and lazy evaluation, Java has not. This is because JavaScript is a functional language and Java is not.
  • In JavaScript you can add properties to an object at runtime, or even change its parent; in Java you can’t. That’s because JavaScript is dynamic and Java is static.
  • In JavaScript, there is no reason to use the new operator. And usually, there is a good reason to avoid it. In Java you can’t even say “Hello World” without new.
  • In JavaScript you always should nest functions. In Java you can’t, and (if you know what Java is) you don’t want to do anything like that.
  • Java has private and protected properties/methods. In JavaScript you encapsulate data in another way.
  • Yes, in JavaScript you can emulate class inheritance. But you should use Prototypes. Don’t pretend you are using classes, because JavaScript doesn’t have classes.
  • a[“b”] is an array in Java, but is an object in JavaScript.

I know that this list is not an explanation of what JavaScript is. But I hope I made clear that JavaScript is NOT similar to Java.
Is JavaScript a subset of Java? No. A dynamic functional language can’t be a subset of a static object oriented language. Unless you think that Haskell is a subset of C++.
Are their syntaxes similar? No. JavaScript syntax is a dirty cheap, because it makes you beilive you are instantiating classes or accessing an element from an associative array, when you’re doing something totally different.

JavaScript is similar to Lisp, not Java. And programming in Java using Lisp is one of the most (hard + stupid + useless) things a developer could try to do. But it’s not your fault. It’s ECMA‘s fault. Ask them why they cheated you.

Leave a comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s