I am frequently reminded by other developers that "JavaScript is not Java!" and that is certainly true. The resemblance between the two languages is limited primarily to the similarity in control flow syntax (for, if, while, etc.) but the same could be said about C and even Fortran77. Both languages support object-oriented programming, but in a different way. Java is strongly typed. JavaScript is loosely typed. Java is compiled. JavaScript is interpreted. Java has lots of rules. JavaScript? Not so many.
If the question is "Are JavaScript and Java similar?", the answer is mostly not. A better question would be "Can JavaScript be an effective tool for object-oriented programming?" Fortunately, the answer is yes. JavaScript has all the tools we needed to support object-oriented programming, even if some of the features are less refined. With a little effort, we can even make JavaScript a bit more hospitable for Java Programmers. That is the goal of this tutorial.
If the question is "Can we make JavaScript work exactly like Java?" the answer is no, and why would we? JavaScript has some weaknesses, when compared to Java, but it also has many strengths. You should think of JavaScript as another useful tool. After all, if the only tool you have is a hammer, every problem looks like a nail! JavaScript is the better tool for some jobs, especially in the area of web development where Java is usually not an option.
This tutorial will discuss techniques to make JavaScript better resemble Java, or at least provide features that Java programmers expect. As an example, we will develop a simple framwork for doing object-oriented programming in JavaScript. The framework will have the following features.
While our example framework is useful, it is intended as a learning example and not production use. There are much more mature frameworks on the market with a rich set of features and large support groups. For example, if you are doing web development you might consider Dojo. If you are doing server-side development in JavaScript, you might consider NodeJS. The one area where our example framework might be useful is when you need a very lightweight framework. The framework consists of about 12KB of code and has extensive comments in Javadoc format. The compressed code is less than 4KB, so it loads quickly.