AJAX - The Complete Reference

(avery) #1

PART IV


Appendix A: JavaScript Quick Reference 559


Given that it may be unclear what the type of a given value is, you may need to employ
the typeof operator.

var x = 5;
alert(typeof x); // displays number

Also, be aware that implicit type conversion can lead to lots of confusion. For example,

alert(5 == " 5 ");

indicates that the two values are equivalent. If you are looking for explicit checking of type
and value, you will need to use the === and !== operators discussed later. Because of the
potential for run-time type errors, explicit conversion is simply more appropriate for safe
programming. Table A-7 details a number of methods to convert types in JavaScript.

NNOTEOT E The Number object also supports methods like toExponential(), toFixed(),
toPrecision() for conversions of numbers to other formats or precision. However, we do not
include them in Table A-7 since they are not changing types per se.

Composite Types


Composite types are collections of primitive types into some larger structure. In JavaScript,
the most generic composite type from which all other composite types are derived is the
object. In JavaScript, an object is an unordered set of properties that may be accessed using
the dot operator:

object.property

For example:

alert(myDog.name);

might be used to access the name property of an object called myDog. Equivalently, this can
be represented in an associative array format:

object[“property”]

So the same example in this case would be:

alert(myDog["name"]);

Generally, the two formats are equivalent, but when accessing properties with spaces in
them or doing some forms of loop the associate array format may be easier to work with.
In the case where we are accessing an object’s property and it is a function, more
appropriately called a method, it may be invoked as:

object.method()

For example, myDog might have a method bark() that could be called like so:

myDog.bark();
Free download pdf