HTML and CSS Reference
In-Depth Information
EXPLANATION
1
The function called weekDay() is defined.
2
The variable now is assigned a number representing the day of the week, where 0
is Sunday.
3
A new Array object called names is created. It will contain seven elements. Each
element will be assigned the name of the weekday (e.g., “Sunday”) .
4
The value in now , a number between 0 and 6, will be used as an index in the names
array. If now is 6, then the value of names[6] , “Saturday” , will be returned.
5
A prototype method called DayOfWeek is assigned the name of the function week-
Day . Now the Date object has a new method that will be inherited by all objects
created from the Date() constructor. The capabilities of the Date object have been
extended to provide a method that will return the name of the weekday. (See
Chapter 8 for more on prototypes.)
6
A new Date object is created with the Date() constructor function.
7
The new prototype method is called, and returns the string value of today's date,
“Saturday” (see Figure 9.23).
Figure 9.23 The day is converted to a string using a prototype.
The Math object allows you to work with more advanced arithmetic calculations, such
as square root, trigonometric functions, logarithms, and random numbers, than are pro-
vided by the basic numeric operators. If you are doing simple calculations, you really
won't need it.
Unlike other objects, you don't have to create an instance of the Math object with the
new keyword. It is a built-in object and has a number of properties (see Table 9.5) and
methods (see Table 9.6). The Math object always starts with an uppercase M.
Table 9.5 Math Object Properties
Property
Value
Description
Math.E
2.718281828459045091
Euler's constant, the base of
natural logarithms
Math.LN2
0.6931471805599452862
Natural log of 2
Math.LN10
2.302585092994045901
Natural log of 10
Continues

Search WWH ::

Custom Search