Wednesday, December 29, 2010

Classes and Inheritance in Javascript

Here is some news for Javascript monkeys: Javascript does not have classes. None. Nada. When people speak of OOP classes and Javascript, they are making it up, literally.

Any talk of OOP classes in Javascript code is a pretense. Now, some of that code is really ingenious, really nifty stuff. But there are no first-class classes in Javascript.

There are two consequence of these non-first-class constructions:

  1. Core language primitives were designed before the faked constructs existed and although the core language retains its closure (as much as before the extensions), the faked constructs introduce new elements and relationships that are not necessarily conserved through the primitive operations. 
  2. The extended constructs do not necessarily meet the same algebraic axioms; the library may not entirely consistent within itself. 
A typical case is the use of JSON to send around objects which use library-based inheritance frameworks. It is easy to reconstruct the property sets, but difficult to ensure that the invariants assumed by your class framework remain invariant after a round trip.
Post a Comment