Using Built In Objects Kevin Harville
Date() instantiation options: birthday = new Date(); //returns now birthday = new Date("June 20, 2001 08:00:00"); birthday = new Date(6, 20, 2001); birthday = new Date(6, 20, 2001, 8, 0, 0); Source: Sams Teach Yourself JavaScript in 24 Hours.
Setting a Date object myDay.setDate(4); myDay.setMonth(0); myDay.setYear(99); //use setFullYear myDay.setFullYear(1999); myDay.setHours(23); myDay.setMinutes(30); myDay.setSeconds(0);
Getting Date Object Info alert(myDay.getDate()); document.write(myDay.getMonth()); myYear = myDay.getYear(); //use getFullYear myDay.getFullYear(); myDay.getHours(); myDay.getMinutes(); myDay.getSeconds();
Math Math is encapsulated in an object format. There are things Math can do - methods, and properties of math, such as PI. Math properties never change Therefore, a function such as raising a number to a power is done via a Math method: myVar = Math.pow ( 2, 4 ); Don't make your own math object. Use the object, Math, javascript provides.
Math Methods Math.random() returns a random number from zero up to, but not including, one.
Math Methods - Rounding Math.round( myVar ) Math.floor( myVar ) Math.ceil( myVar )
Math Methods - Trigonometry Math.acos(x) Math.asin(x) Math.atan(x) Math.cos(x) Math.sin(x) Math.tan(x) IMPORTANT - THESE CALCULATIONS ARE DONE IN RADIANS. A SIMPLE BUT NECESSARY CONVERSION FROM RADIANS TO DEGREES NEEDED.
Math Minimum / Maximum Math.max ( valA, valB ) Math.min ( valA, valB)
Math Powers and Roots Math.pow ( num, pwr ) Math.sqrt ( 25 )
Other Math Methods Other Math methods exist. Consult your reference materials when doing higher math.
Math Properties Math.E Math.PI Math.LN2 Math.LN10 Math.LOG2E Math.SQRT1_2 Math.SQRT2 The Math object's properties are Math constants. The only Math property I want you to know for this class is Math.PI
Browser Objects
Document Object Model window location document history anchors [ ] links [ ] forms [ ] images [ ] elements [ ]
Things you can do with DOM Refer to document properties Clear a document Refer to and modify specific anchors and links. Move to and refer to previous and next pages. Move to a specific new page
Things you can do... You can do much more. The details in this chapter were only briefly covered. Read Chapters 9 and 10. All this is done by using the Document Object Model and object-oriented programming.