Objects as Variables Featuring the Date Object Copyright © 1997 - 2015 Curt Hill
Introduction Recall the five basic types: Number String Boolean Undefined Null If it is not one of these, what is it? One of the possibilities is an object Copyright © 1997 - 2015 Curt Hill
Handles Objects are always referred to by a handle A handle is just a pointer Although perhaps safer than pointers in C/C++ The handle is on the stack and refers to the value which is on the heap An object must be initialized with the new operator Copyright © 1997 - 2015 Curt Hill
The new operator Three functions performed by new: Allocate memory on the heap Invokes memory manager Generate the address to this memory This is usually received by a handle variable Call the constructor for the object Constructors initialize Copyright © 1997 - 2015 Curt Hill
Form of New new object_name(parm) new is an operator One of the few that is a word object_name is the name of the kind of object parm is the parameter to the constructor May be left out in case of default constructor Usually result is assigned to variable Copyright © 1997 - 2015 Curt Hill
The Date object A Date represents a date and time Stored as a number This number may represent any date within 100 million days of January 1, 1970 This is approximately 285,616 years It handles this as an extrapolated Gregorian calendars Thus it believes in October 10, 1582 October 5-14 were skipped in 1582 when calendar was started Copyright © 1997 - 2015 Curt Hill
A constructor example Suppose that we have the following code: var d; // Set to undefined d = new Date(); d2 = new Date; document.write("<P> ",d); Both d and d2 use the default constructor Result is: Wed Jan 30 2008 20:51:01 GMT-0600 (Central Standard Time) Copyright © 1997 - 2015 Curt Hill
What do I get? The default constructor for Date initializes it to the current time from the system clock Most objects convert to a string in an object specific fashion With Date you get the string given above, which indicated date, time and timezone Copyright © 1997 - 2015 Curt Hill
Date constructors Date may be initialized with a succession of information: Year, Month, Day, Hour, Minute, Second, Milliseconds Month is 0-11, not 1-12 Provided in this order – as many or as few A copy constructor also exists Copyright © 1997 - 2015 Curt Hill
Examples d1 = new Date; d2 = new Date(); d3 = new Date(2008,2,1); // March 1 d4 = new Date(d3); d5 = new Date(2008); // Jan 1 d3 = new Date(2008,2,1,10,30); Copyright © 1997 - 2015 Curt Hill
Operators Generally operators work on the handles Methods should be used for object access or manipulation Assignment: d2 = d1; // copies handle d3 = new Date(d2); // copies // contents Copyright © 1997 - 2015 Curt Hill
Date Methods There are a number of get and sets that allow access and manipulation Setting a value setYear – 1 to 3 parameters setMonth – 1 or 2 parameters setDate setHours – 1 to 4 parameters setMinutes – 1 or 3 parameters setSeconds– 1 or 2 parameters setMilliseconds Copyright © 1997 - 2015 Curt Hill
Getting a value The expected methods: getYear getMonth getDate etc All take no parameters and return a number A few less expected: getDay getTimeZoneOffset Copyright © 1997 - 2015 Curt Hill
Examples Four days ago: d2.setDate(d2.getDate()-4) Particular date d2.setYear(2007,11,15); Copyright © 1997 - 2015 Curt Hill
What can be done to any object? Execute any of the objects methods Assign to a similar object Direct comparison However, assignment and comparison are on the handle not the actual object Copyright © 1997 - 2015 Curt Hill
Assignment Assignment of two object does not necessarily create new instances It merely copies the handle This can cause problems because of side effects Consider d1 = new Date(2007,0,21); d2 = d1; // Handle assign d2.setMonth(0,2); Copyright © 1997 - 2015 Curt Hill
Date Example d1 = new Date (2007,0,21); d1 January 21, 2007 d2 d2 = d1; d1 January 21, 2007 d2 d2.setMonth (0,2); d1 January 2, 2007 d2 Copyright © 1997 - 2015 Curt Hill
One more thought on assignment Since direct assignment on objects only copies the handle it is better to use the copy constructor Provided it exists Thus avoid: d2 = d1; and use: d2 = new Date(d1); where possible Also use this for value parameters Copyright © 1997 - 2015 Curt Hill
Comparisons Same problem with comparisons on handles So with: d3 = new Date(d2); d2 will not compare equal with d2 d2 == d3 is false because the handles are different, not the object contents Copyright © 1997 - 2015 Curt Hill
valueOf method Returns the number that is the basis of date Comparing this number will give reasonable results Thus d3 = new Date(d2); d3.valueOf() == d2.valueOf() will be true In most other cases the number is of dubious value Copyright © 1997 - 2015 Curt Hill
Memory partitions The memory for programs is divided into three chunks The code memory Machine language instructions The stack memory Basics and handles The heap memory Objects Copyright © 1997 - 2015 Curt Hill
Basic types are on the Stack Allocated at declaration or first use Deallocated at end of method that contains declaration Quick allocation and deallocation Only primitives and handles on stack Stack also includes return addresses, function results and temporary results Copyright © 1997 - 2015 Curt Hill
Objects are on the Heap Allocated by specific use of new keyword Deallocated by the delete operator Requires a handle (pointer) All objects are on heap and nothing but objects on heap Copyright © 1997 - 2015 Curt Hill
delete Disposes of a handle Form is: delete variable; It returns a boolean value Copyright © 1997 - 2015 Curt Hill
Memory Heap Stack s = 5; 5 January … t=null; null u= new Date() c = ‘ABC’; ABC Copyright © 1997 - 2015 Curt Hill
Java and JavaScript Java has primitives, but JavaScript has Basic types The JavaScript basics are really objects They just do not need a new or delete They all have methods and properties Copyright © 1997 - 2015 Curt Hill