Why JavaScript is a Jem
Written by Ian Elliot   
Thursday, 02 December 2010
Article Index
Why JavaScript is a Jem
Dynamic inheritance

December's I Programmer Poll ask for your opinion of JavaScript. Here our JavaScript editor, Ian Elliot, provides his perspective.


I was asked recently if I could justify why I am so enthusiastic about JavaScript - and I discovered it was quite a subtle explanation.

Misunderstood and misused

JavaScript is a much under-rated language.

The reason is probably that it seems to be a scripting language aimed at non-programmers. As a result most JavaScript programs are horrible to look at and use few of the more interesting language facilities. Javascript can be written in a one instruction after another procedural form and it often is.

Beginners treat it as a natural progression from tinkering with HTML and often pick it up as they go along. As a result not only is the typical JavaScript program lacking style, it often doesn't work too well either.

Unfortunately other problems arise when the experts get hold of it. JavaScript so flexible that you can turn it into almost anything you want to – so you will find JavaScript cast as C, C++, Lisp, Forth, Scheme, etc.
It can be used in a procedural fashion, object oriented, functional, declarative or any discipline you care to apply to it,  admittedly with variable amounts of success.

To compound all of these problems the official specification is very poor and gives little guidance on how the language should be used. This all leaves the poor programmer wanting to use JavaScript in elegant ways no option but to wade through exemplar code, usually in the form of widget libraries or similar. 

Philosophy, style and heritage

Yet the fact of the matter is that JavaScript isn't a crude scripting language with nothing but simple datatypes and basic verbs. It is a language with a philosophy and a style of its own. It even has a heritage in that it is based on Self - a prototypical object oriented language and Scheme a functional Lisp derivative. With parents like these JavaScript can hardly be called "just a scripting language". However it would be  unrealistic not to recognise that the language has had to cope with limited resources early in its development and a demand for it to be dumbed down so that non-programmers might manage to make some use of it. As a result JavaScript is flawed but arguably it's still enough of a jem to admire.

Object-oriented and dynamic

So what is special about JavaScript?

The first thing to say is that it is object oriented but its objects are dynamic. That is, you can create an object and add methods and properties to it at run time. Indeed you can even remove methods and properties making the JavaScript object completely plastic. This in turn means that JavaScript has a very weak notion of type. As an object can morph from one "type" to another by gaining and losing properties as required there is no real notion of a type hierarchy and no need for one. In addition there is no need and no provision for the class/object distinction.

In traditional object-oriented languages objects are created by first defining a class - a blueprint for an object. Then you create an instance of the class, i.e. an object. This is not a bad way to work with static objects and it fits in well with strong static typing, which is an approach many programmers think is good.

JavaScript on the other hand simply allows you to create an object - without the need for a class to act as a template. 


In principle JavaScript is also a prototype based language where new objects can be created from existing objects, but in practice this is probably one of its weakest areas. Prototypes play the role of inheritance in a Prototype language - one object serves as the basis for another object providing it with a default set of properties and methods.

JavaScript implements prototypical inheritance in a complicated way that makes use of a "helper" object, a function which acts as an object factory. The object factory creates instances of an object which provides a sort of basis for a type system - anything a given object factory creates is regarded as an instance of the same type. The only problem with this is that the instance can have properties and methods added or deleted at run time and so being of a particular type still doesn't provide much information about the object unless you add some self-imposed rules.


Javascript Jems on Objects

A complete introduction to Javascript and objects. This article is part of a series that explains how Javascript has a very special approach to objects. You might like to read them in order.

1.   A new take on objects

2.  Object factories, constructors and clones

3. Type and the constructor

4. Javascript Jems - The Prototype
5. Javascript Jems - Prototype Inheritance




Last Updated ( Friday, 03 December 2010 )