|Written by Ian Elliot|
|Thursday, 21 August 2014|
Page 1 of 3
There is a newer version of the draft of the book here.
The Classical Constructor
It might help to look first at how class based languages deal with the problem of creating instances. If you don't know about class based languages work just skip to the next section.
In class based languages instances of a class are generally created using the constructor which implicitly uses the class definition to manufacture an instance of the class.
For example in language like Java, C++ or C# you would write:
where myClass() is the constructor. The constructor is a function that returns an instance of the class.
This is a fact that is often overlooked by programmers in class based languages because they don't actually have to write any thing in the constructor for it do do its job. That is an empty or default constructor will automatically do the job of creating an instance. This results in programmers thinking that constructors are really only needed when something has to be initialized or setup in some way.
In fact the idea of a constructor can be extended to the idea of an object factory. An object factory is an object that will create another object - perhaps another instance of its own type or another completely different type.
The idea of a function which creates objects is core to class based languages and to all object oriented languages.
If you just want a single object then you could simply make use of an object literal and define your object.
This works perfectly well and it is a good way to make a singleton object - i.e. an object that there should only be one instance of in any given program.
For example if you want to store the position of a single 2D point you might define:
How do object factories work?
At their very simplest an object factory creates an object literal and returns it as its result.
For example an object factory for the point object is:
and to use it to create an instance of the Point object you would use:
It seems trivial yet in no time at all it can become complicated and subtle.
Notice that we already have recreated one of the minor problems of a class based language - finding a name for the class and for an instance. In this case the naming problem is finding something to call the object factory and then names for the instances - i.e. Point and myPoint. Perhaps it would be better to call the factory PointMaker or something to indicate that it wasn't actually an instance of a Point object.
As the object being created gets bigger and more complicated, building it in smaller steps becomes a better method. Starting from an empty object you can add properties one by one.
One of the big advantages of using an object factory is that you can use parameters to customize the object produced.
and you create the object using:
The ability to customize object instances is one of the big plus points of using an object factory. It is so useful that an object factory is often used to create a singleton - even though the factory will only ever create a single object.
Of course the object that the factory produces can have any type of object as its properties including Function objects.
To make a Function property act like a method you have to make use of the this call context, i.e. this, to specify the instance that is being referenced.
If you are creating an object as an object literal there is no other choice but to use this to create a late bound method for an object.
However if you are using a factory function there is an alternative and it is arguably better as it creates an early bound method.
For example, suppose we want to write a method that adds x and y together:
Notice that the function uses obj.x and obj.y not just x and y.
If you try out
you will find it works and displays 6 - but how can it?
When myPoint.add is called, the variable obj has long gone as the factory object's code has terminated and all its variables have been destroyed. How can a Function object make use of the factory's obj variable long after it has been destroyed?
The answer, of course, is closure.
Also notice that code in other Function objects can't access the obj variable because it no longer exists and it is local to the factory function. That is, obj is a private variable for the instance of the point object we have created.
The general rule is that any local variables declared within the object factory are turned into private variables for the object created by closure.
This is another advantage of using an object factory even for a singleton. Object literals don't have private variables unless they are created within a function.
|Last Updated ( Sunday, 10 May 2015 )|