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

Banner

Inheritance?

The object factory also provides the mechanism of prototype inheritance. Any object created by an object factory uses the object factory's prototype property in an attempt to find any method or properties it doesn't implement directly. By setting the object factory's prototype property to another object you can implement inheritance and by prototype chaining you can also implement an inheritance hierarchy. This might sound like standard object-oriented inheritance but it has its dynamic aspects. You can add properties and methods to the prototype object at run time and so what is inherited can change at run time.

This is inheritance but not as we know it.

The problem is that the whole object factory and its role in inheritance and type isn't well thought out and it isn't complete. What is more it isn't well enough understood by the average or even expert JavaScript programmer to really be used in a way that doesn't just mimic a broken implementation of classical inheritance.

JavaScript's dynamic object are almost certainly best used without the help of type and minimal use of classical inheritance. The problems that it seems to have are mostly due to the way we attempt to use it.

A functional language

The second big aspect of JavaScript that tends to be overlooked is that it is a functional language. In it functions are "first class objects" - they have methods and properties and can be passed into and returned by functions like any other datatype. Essentially a function is a object that carries a special method that is executed by the invoke operator i.e.  a pair of round brackets ().

As well as first class functions JavaScript also supports closures, recursion and dynamic modification of code.

Closures are something that most beginners don't understand and when they do understand they have no idea how to make use of them. Closures make asynchronous programming i.e. event handling, much easier and cleaner. If you look at most event handling in JavaScript you will find that this isn't the way that it is done however - another example of how JavaScript is misunderstood by the majority of users.

If closures are generally misunderstood or misused then what can you say about recursion and code modification. Recursion is a problem for the average programmer in almost any language and JavaScript is not exception. You can use it but do you know how to use it?

JavaScript does, however, lack a lot of very basic functional programming features. It basically lacks most of the convenient functions that are standard in most functional languages such as map, reduce or fold. These can be provided very easily by suitable JavaScript libraries, but this isn't really the same as having them as part of the core language.

Extensibility

The fact that you can add many of the standard features of functional programming using a library brings us to the final topic - extensibility. JavaScript is an example of a minimal but extensible language. You can create JavaScript functions that mimic the features of just about any language. This has resulted in lots of big and useful JavsaScript libraries - jQuery, script.aculo.us, Prototype and so on. The only problem with this is that there isn't one big official JavaScript framework that you can devote your attention to. Choice isn't always a good thing in programming language terms.

Shortcomings - well yes

At the end of the story I have to be fair and point out that JavaScript has a lot wrong with it. It could do with some additional features such as access modifiers, threading, and the better facilities to manipulate the prototype pointer and control evaluation context. However all of these are small additions that could be made without fundamentally changing the language. What is a more serious lack is the sort of IDE support we take for granted for "serious" languages such as Java or C#.

In many ways it is not JavaScript that needs changing but its implementation. 

Just remember the next time you approach a JavaScript task - you are working with a dynamic, untyped, functional,  prototypical, object-oriented language and not "just a script".


Banner


JavaScript Jems - The Inheritance Tax

JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way.  In particular, it doesn't do inheritance  [ ... ]



JavaScript Jems - Objects Are Anonymous Singletons

JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way.  In particular, every object can be regard [ ... ]


Other Articles

<ASIN:0596004117>

<ASIN:0596806752>

Last Updated ( Friday, 03 December 2010 )