|Written by Ian Elliot|
|Wednesday, 09 March 2011|
Page 1 of 2
It has many radical features which, if it had been presented as a new exciting language, would have won it many friends.
Instead it was introduced as a workhorse scripting language that has been misunderstood by many, often inadequate, programmers.
Even the competent programmers who have had the pleasure of getting to know it have usually attempted to reconstruct the image of their favourite language by introducing features like class and inheritance or functional constructs like map and reduce.
Objects not classes
or you could use an object literal or a new Object declaration as alternatives. The point is that no matter how you create an object you are doing it directly and without the need or use for the idea of a class.
There are three main reasons. The first is that a class definition is used to create multiple instances of an object. The second is that class is used to define type and third inheritance is implemented via class.
Let's look at how each of the class requirements can be met in an interpreted dynamic language.
To create as many copies of an object as you like you simply write a function that creates the object and returns it:
Now you can "stamp out" as many copies of the object as desire:
and so on.
Notice that using a factory function extends the range of possible behaviours an object can have. The whole thing works because of closure - the myTempObject variable is still available to the methods of the object even though it has gone out of scope. It acts like a private variable which references the object i.e. a stand in for "this". The same mechanism can be used to implement additional private variables that can be used for any purpose -simply define them using var and use them in the objects methods.
Notice that there is no notion of static or class variables because there is no need for the idea of class only object.
As a small aside it is worth mentioning that there are many ways of creating a class factory. You can use "this" in place of the private object variable myTempObject in the example or you could go the whole way and write a constructor and use new. What matters at this point is not the exact way you create an object using a function, but the simple fact that you do it without having to invent the concept of class.
Type or the lack of it
So objects are created and constructed as part of code at run time. Now we turn out attention to type. For primitive things like integers, floats and chars type seems like a really good idea because it defined what you can do with them - addition or concatenation. This basic idea of data type is later extended to objects where objects of the same type have the same properties and methods. It is then later extended even further into a type hierarchy where derived type has all of the properties and methods of its parent and some of its own. The point being that you can always treat a derived type as being of its parent type because it has sufficient methods and properties to be used in its place.
How does this typeless object environment change things?
Surprisingly the answer is not much but it is a big simplification.
Instead of having to worry about object hierarchies, up casting, down casting and so on all you have to worry about is - does it have the method/property I'm about to use.
You don't need a concept of type to ensure that an object has a method or property that you are about to use.
|Last Updated ( Thursday, 10 March 2011 )|