The Problem with Compiling JavaScript

By Jeff Cogswell  |  Posted 2008-09-17 Print this article Print


The Problem with Compiling JavaScript and Other Dynamic Languages

If you've programmed in C++, you're well aware that before you can access a class, you need to have the class fully defined. Typically you'll put your class definition in a header file. This definition includes the word "class," the name of the class, and a list of all the member variables and member functions. Later in your code you can create objects that are instances of the class.

The class is much like a cookie cutter for objects. Once the program is running  and you create an object, the object has the members it will have until it is destroyed. You can change the value of the members (such as changing the height member of a Rectangle object from 10 to 20 or changing the balance member of the CheckingAccount object from 100 to 100,000,000) but you can't add or remove members. While your program is running, you can't add a depth member to your Rectangle class. The only way you can make changes is by changing the code and recompiling. But then when you run the program, all your Rectangle objects will have the new member.

JavaScript, however, is a dynamic language, and the whole concept of classes changes drastically. You can add and remove members of an object while the program is running. Thus, you can have two Rectangle objects, for example, and then add a depth member to just one of those objects.

In fact, although it's not necessarily the best programming practice, you can even provide the user of your program with a text box where he or she can type in a name, and then you can add a member to an object by that name. Thus, your user could suggest the name eWEEK for a member, and you could add that member to the object and give it a value.

And further, that value can be any type you want-again something totally different from C++. In C++, if you have a Rectangle class with members Height and Width and you specify that they are to hold integers, then you can't store a string in one of these members while the program is running. In fact, the compiler will catch it and stop you while you're compiling before you're even able to run the program.

But not so with JavaScript: You can store the number 10 in your eWEEK member, or you can store the string "Awesome Publication" in the member.

As you can probably imagine, this makes for some serious problems with compilation. Microsoft, for example, hit these snags when trying to add dynamic languages to the list of languages that can be compiled under the .NET run-time. Microsoft's CLR (Common Language Runtime) is strongly typed and not dynamic, making languages such as JavaScript and Python difficult to compile. Microsoft now has support (primarily in its Silverlight product) for dynamic languages, but to do so it had to add an entire dynamic language layer on top of the run-time.

(And incidentally, just to make life even more interesting, almost anything in JavaScript can be treated as an object and have members assigned to it at run-time, including even functions.)

Google has taken a unique approach to compiling JavaScript. It's implemented something known as hidden classes.

Jeff Cogswell is the author of Designing Highly Useable Software ( among other books and is the owner/operator of CogsMedia Training and Consulting.Currently Jeff is a senior editor with Ziff Davis Enterprise. Prior to joining Ziff, he spent about 15 years as a software engineer, working on Windows and Unix systems, mastering C++, PHP, and ASP.NET development. He has written over a dozen books.

Submit a Comment

Loading Comments...
Manage your Newsletters: Login   Register My Newsletters

Rocket Fuel