Object-oriented JavaScript

For all its warts, JavaScript can be used as an object-oriented programming language. It supports data encapsulation easily, polymorphism comes naturally, and inheritance can be had with some work.

However, one must know the issues and play some tricks to make it happen. Although JavaScript was plainly meant to be a bona-fide object-oriented language, its object-oriented features are almost more of a hindrance than a help. I would call JavaScript an attempt at an object-oriented language. There are languages that were not originally intended to be object-oriented, that pull it off better than JavaScript.

The programming techniques are peculiar to JavaScript, and not obvious. The official JavaScript documentation does not explain how to do inheritance properly—the effect would not be at all what a programmer would want.

These pages discuss the elements of object-oriented programming in JavaScript, but not what object-oriented methodologies are about. If you don’t have a clue about object-oriented analysis, design and programming, these pages will not help you—pick up a book on the subject. Also, although the purpose of JavaScript is to manipulate web pages in a browser, these pages won’t explain how to do that either.

Readers familiar with object-oriented programming, but not in a scripting language environment, should take note of two important language features of JavaScript that are absent in, say, C++ or Java.

First, in JavaScript (as in Perl and Python), member variables and functions may be added to an object on the fly, after it has been instantiated, and so these languages are said to exhibit dynamic polymorphism. (There are mechanisms for checking whether a given object has a desired member.)

Accordingly, there is no class declaration in JavaScript (what purpose would it serve?). Instead, the initial content of an object is specified by its constructor.

Second, JavaScript supports nested functions, that is, functions defined within the body code of another function. Variables in the scope of the containing function are also in the scope of the nested functions.

Nested functions may be accessed by code outside their containing function, and be called after the containing function exits. The variables in the scope of the containing function persist so long as the nested functions that refer to them persist. By contrast, in C++, variables defined within a function cease to exist (before or) when the function exits. Of course, there is nothing like a nested function in C++.

Member functions of JavaScript objects are implemented as nested functions of the object constructor function.

Inheritance in JavaScript is accomplished by the constructor copying members from a prototype object of a parent class. JavaScript is therefore called a prototype-based object-oriented language as opposed to class-based object-oriented languages, such as Java and C++.

The JavaScript syntax relating to objects is not large. There is a special type, Object, to which all JavaScript objects belong. There are four keywords: a variable this referring to an object, defined within code belonging to the object, an operator new which creates a new object using the constructor supplied to it, and an operator delete which un-defines a member variable, and an operator instanceof which tests whether a given object inherits from a given constructor. All JavaScript functions have a property prototype, that allows them to be used as a constructor to create objects inheriting from a prototype object and methods call and apply, that permit passing arguments from child constructors to parent constructors.


It is necessary to keep a copy of the JavaScript documentation handy: Developer manuals at Mozilla.

The ECMAScript Language Specification is much harder and not geared for practical use. For folks who did some computer science: ECMA-262

Dreaming in color