ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾JavaScript£¾JavaScript Notes

Using Built-in Objects and Functions

The JavaScript Language contains the following built-in objects and functions:

These objects and their properties and methods are built into the language. You can use these objects in both client applications with Netscape Navigator and server applications with LiveWire.

Using the String Object

Whenever you assign a string value to a variable or property, you create a string object. String literals are also string objects. For example, the statement

mystring = "Hello, World!"

creates a string object called mystring. The literal "blah" is also a string object.

The string object has methods that return:

  • a variation on the string itself, such as substring and toUpperCase.
  • an HTML formatted version of the string, such as bold and link.

For example, given the above object, mystring.toUpperCase() returns "HELLO, WORLD!", and so does "hello, world!".toUpperCase().

Using the Math Object

The built-in Math object has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi, which you would use in an application as

Math.PI

Similarly, standard mathematical functions are methods of Math. These include trigonometric, logarithmic, exponential, and other functions. For example, if you want to use the trigonometric function sine, you would write

Math.sin(1.56)

Note that all trigonometric methods of math take arguments in radians.

It is often convenient to use the with statement when a section of code uses several math constants and methods, so you don't have to type "Math" repeatedly. For example,

with (Math) {
   a = PI * r*r;
   y = r*sin(theta)
   x = r*cos(theta)
}

Using the Date Object

JavaScript does not have a date data type. However, the date object and its methods enable you to work with dates and times in your applications. The date object has a large number of methods for setting, getting, and manipulating dates. It does not have any properties.

JavaScript handles dates very similarly to Java. The two languages have many of the same date methods, and both languages store dates as the number of milliseconds since January 1, 1970 00:00:00.

NOTE: You cannot currently work with dates prior to 1/1/70.

To create a date object:

varName = new Date(parameters)
where varName is a JavaScript variable name for the date object being created; it can be a new object or a property of an existing object.

The parameters for the Date constructor can be any of the following:

  • Nothing: creates today's date and time. For example, today = new Date()
  • A string representing a date in the following form: "Month day, year hours:minutes:seconds". For example, Xmas95= new Date("December 25, 1995 13:30:00") If you omit hours, minutes, or seconds, the value will be set to zero.
  • A set of integer values for year, month, and day. For example, Xmas95 = new Date(95,11,25)
  • A set of values for year, month, day, hour, minute, and seconds For example, Xmas95 = new Date(95,11,25,9,30,0)

The Date object has a large number of methods for handling dates and times. The methods fall into these broad categories:

  • "set" methods, for setting date and time values in date objects
  • "get" methods, for getting date and time values from date objects
  • "to" methods, for returning string values from date objects.
  • parse and UTC methods, for parsing date strings.

The "get" and "set" methods enable you to get and set seconds, minutes, hours, day of the month, day of the week, months, and years separately. There is a getDay method that returns the day of the week, but no corresponding setDay method, because the day of the week is set automatically. These methods use integers to represent these values as follows:

  • seconds and minutes: 0 to 59
  • hours: 0 to 23
  • day: 0 to 6 (day of the week)
  • date: 1 to 31 (day of the month)
  • months: 0 (January) to 11 (December)
  • year: years since 1900

For example, suppose you define the following date:

Xmas95 = new Date("December 25, 1995")
Then Xmas95.getMonth() returns 11, and Xmas95.getYear() returns 95.

The getTime and setTime methods are useful for comparing dates. The getTime method returns the number of milliseconds since the epoch for a date object.

For example, the following code displays the number of shopping days left until Christmas:

today = new Date()
nextXmas = new Date("December 25, 1990")
nextXmas.setYear(today.getYear())
msPerDay = 24 * 60 * 60 * 1000 ; // Number of milliseconds per day
daysLeft = (nextXmas.getTime() - today.getTime()) / msPerDay;
daysLeft = Math.round(daysLeft);
document.write("Number of Shopping Days until Christmas: " + daysLeft);

This example creates a date object named today that contains today's date. It then creates a date object named nextXmas, and sets the year to the current year. Then, using the number of milliseconds per day, it computes the number of days between today and nextXmas, using getTime, and rounding to a whole number of days.

The parse method is useful for assigning values from date strings to existing date objects. For example, the following code uses parse and setTime to assign a date to the IPOdate object.

IPOdate = new Date()
IPOdate.setTime(Date.parse("Aug 9, 1995"))

Using Built-in functions

JavaScript has several "top-level" functions built-in to the language. They are:

  • eval
  • parseInt
  • parseFloat

The eval Function

The built-in function eval takes a string as its argument. The string can be is any string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.

If the argument represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements.

This function is useful for evaluating a string representing an arithmetic expression. For example, input from a form element is always a string, but you often want to convert it to a numerical value.

The following example takes input in a text field, applies the eval function and displays the result in another text field. If you type a numerical expression in the first field, and click on the button, the expression will be evaluted. For example, enter "(666 * 777) / 3", and click on the button to see the result.

<SCRIPT> function compute(obj) { obj.result.value = eval(obj.expr.value) } </SCRIPT> <FORM NAME="evalform"> Enter an expression: <INPUT TYPE=text NAME="expr" SIZE=20 > <BR> Result: <INPUT TYPE=text NAME="result" SIZE=20 > <BR> <INPUT TYPE="button" VALUE="Click Me" onClick="compute(this.form)"> </FORM>

Enter an expression:
Result:

The eval function is not limited to evaluating numerical expressions, however. Its argument can include object references or even JavaScript statements. For example, you could define a function called setValue that would take two arguments: and object and a value, as follows:

 function setValue (myobj, myvalue) {
   eval ("document.forms[0]." + myobj + ".value") = myvalue;
 }

Then, for example, you could call this function to set the value of a form element "text1" as follows:

setValue(text1, 42)

The parseInt and parseFloat Functions

These two built-in functions return a numeric value when given a string as an argument.

ParseFloat parses its argument, a string, and attempts to return a floating point number. If it encounters a character other than a sign ( + or -), numeral (0-9), a decimal point, or an exponent, then it returns the value up to that point and ignores that character and all succeeding characters. If the first character cannot be converted to a number, it returns NaN.

The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. If the first character cannot be converted to a number in the specified radix, it returns NaN. ParseInt truncates numbers to integer values.