Thursday, May 20, 2010

Notes about differences between Java and JavaScript

Dynamic binding VS Static binding
JavaScript features DYNAMIC binding, so all object references are checked at run time. Java, on the other hand, is based on STATIC binding, meaning that all object references must exist and be valid at compile time. However, an object-oriented language may require dynamic method bindings because polymorphism allows multiple definitions of methods sharing a common name, and calling such polymorphic methods often cannot be resolved until run time. The most obvious reason for this difference is that JavaScript is NOT compiled, so checking object references at compile time has no meaning.

Object-oriented VS Object-based
JavaScript is based on a simple object-oriented paradigm. This paradigm is often called object based, as opposed to object oriented. If you are used to a truly object-oriented language such as C#, Java, or C++, you will find much of that functionality is missing in JavaScript. For example, classes do not exist in JavaScript (all objects belong to one “class”), nor do packages (because a package groups classes together). The object hierarchy in JavaScript is a containment hierarchy, not an inheritance hierarchy as in Java and C++. That is, an object does not inherit from other objects, but it can be contained by another object if it is a property of that object. Most object-oriented languages require static resolution of objects at compile time.
However, an object-oriented language may require dynamic method bindings because polymorphism allows multiple definitions of methods sharing a common name. Calling such polymorphic methods often cannot be resolved until run time. JavaScript is completely based on dynamic binding. That is, object references are checked at run time. There are many other differences between the object paradigm in JavaScript and the one in full object-oriented languages (such as Java and C++).

Reference: Advanced JavaScript by Chuck Eastom