Page 1 of 3
- The Associative Array
- The String Object
- The Array object
- A Time Interval Object
- Collection Object
- Stacks, Queue & Deque
- The Linked List
- A Lisp-like list
- The Binary Tree
- Bit manipulation
- Typed Arrays I
- Typed Arrays II
* First Draft
- inheritance as augmentation
- creating an "overloaded" constructor
- identifying the the new object type
The key idea is that we are going to create a special type of Date object that stores the time interval as a number of milliseconds.
The object augmentation pattern:
- create a factory function for the new object
- use the existing factory function to create an instance of the old object
- use the new factory function to add any additional properties of methods you need and return the augmented instance.
Let's use the augmentation pattern to create a TimeInterval object from the Date object.
We first need a new factory function for the TimeInterval object:
All this has to do is create and return a Date object:
var obj=new Date();
That is the new factory function is:
var obj=new Date();
Now all we have to do is add the new properties and methods we require for the TimeInterval object. Notice that this is an object factory, rather than a constructor, and you don't have to use the new keyword with it. If you do use the new keyword it doesn't make any difference - it just isn't necessary as the function creates its own object to return rather than relying on the default object.
The overloaded factory function
The first thing that we would like to do is create a factory function that is flexible in the way itinitializes the new object. That is, we would like to be able to write:
- TimeInterval(mill) to initialize the object to ticks milliseconds.
- TimeInterval(h,m,s) to initialize the object to the specified hours, minutes and seconds
- TimeInterval(d,h,m,s) to initialize the object to the specified number of days, hours, minutes and seconds.
- TimerInterval(d,h,m,s,mill) to initialize the object to the specified number of days, hours, minutes, seconds and milliseconds.
The key to creating a flexible factory function is to work with the arguments array directly to work out what parameters have been specified.
In this case we can do the job with a few if statements:
var d=0, h=0, m=0,s=0,mill=0;
So depending on the number of arguments we set the variables d, h, m, s and mill accordingly. You might wonder why we use valueOf? The simple reason is that this allows the user to specify any object that has a numerical value. For example without valueOf the statement:
var t=TimeInterval(new Date());
wouldn't be allowed as you are expecting an integer number of milliseconds. However in this case the Date().valueOf() returns an integer value which is the number of milliseconds from the fixed date.
At the end of all of the if statements we have a specification of the time interval in terms of d,h,m,s and mill and we need to convert this into milliseconds and use it to create a Date object:
var obj=new Date(t);
Now the object factory returns a Date object initialized to the correct time interval in milliseconds.