![]() |
|
#1
|
|||
|
|||
|
The following fragment:
var d = new Date writeLog(d.toLocaleString()) produces output similar to the following. Fri Oct 23 10:29:05 1998 The first line creates a variable d as a new Date object, or more accurately, as a new instance of a Date object. The second line uses the Date toLocaleString() method of the Date object to display local date and time information. This batch script could be written as a program script as shown in the following fragment. function main() { var d = new Date; writeLog(d.toLocaleString()); } The main() function, if it exists, is the first function to be executed in a script. This script, using a structured programming style, produces the exact same result as the first two lines, which follow a batch style. The following fragment is another variation that produces the same result. var d = new Date; function main() { writeLog(d.toLocaleString()); } Remember that lines of script outside of functions are executed before the main() function. The following fragment is yet another variation. function main() { DisplayTime(); } function DisplayTime() { var d = new Date; writeLog(d.toLocaleString()); } To repeat, the first fragment shown consists of two lines of code written as a simple batch script. The fragments, shown after it, are all written as program scripts. All of the fragments accomplish the same thing, namely, displaying local day, date, and time information. All fragments work equally well. What are the differences? If a user wanted a simple script to display date and time information, then the first batch script would likely be the best choice. However, if a user wanted to write a more involved program, one in which the display of the date and time was only a small part, then one of the program scripts would be the best choice. Remember, Javascript scripts may be as simple or as powerful as a user chooses. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|