|Tuesday, 07 September 2010|
Page 1 of 3
In the next and subsequent installments we look at the central role that the function object plays in creating object factories and the more specialised object constructors. Finally we will reach something you might well recognise, but when we do you will see it in a new light and be able to invent new ways of working with objects.
But to start at the beginning...
Parts of this story you will almost certainly already know - what matters is the way that they connect together.
Class v Prototypical objects
There are two very general approaches to creating objects in object oriented languages.
You can go about it indirectly and create a “class” from which you instantiate as many objects as you need.
The class acts as a sort of tool that can be used to stamp out copies of the object.
Many beginners find this difficult because they are focused on producing something that “does something” rather than working one stage removed – class then object.
A prototype language, on the other hand, doesn’t bother with classes at all – it’s class free.
What happens is that the programmer creates objects by writing the code that implements a single object.
Beginners tend to like this as it is direct – you want an object so you write the code for the object, no mucking about with classes that simply get in the way and cause problems with thinking up names.
For example, if you need a stack object in a class-based language you first have to implement a class called CStack and then create an instance of a stack object something like:
CStack Stack=new CStack()
In a prototype language you don’t have to invent CStack as a template for the stack object you simply write the code that is within CStack as if it was the Stack object and get on with using it.
Of course creating a single instance of an object doesn’t really give you the full benefit of an object-oriented approach but prototype languages solve this by providing the ability to clone objects.
Instead of stamping out identical objects using a class you simply create more objects by cloning existing objects. Once you have the cloned object you can change its behaviour by adding methods and properties to it.
In fact most prototype languages don’t bother providing a facility to create a new object, they simply provide some built-in objects that you can clone to create your own objects.
So to be clear:
Creating and using an object
Let's start at the beginning and the most appropriate beginning.
You can create an object simply by writing down the properties and methods it should have. The null object, i.e. an object with no properties or methods is simply:
The null object is sometimes a useful starting point because we can add properties and methods to it later on. Let's move on to consider an object with a single property and a single method:
var myObject =
You probably already know the basic syntax. Each property or method is defined as part of a comma separated list as a set of name:value pairs. So a general object is created using:
var myObject =
And we are using the view that a method is simply a property that just happens to have a function as its value.
|Last Updated ( Monday, 04 October 2010 )|