Composite Types


Objects form the basis for all nonprimitive types in JavaScript. An object is a composite type that can contain primitive and composite types. The main distinction between primitive types and composite types is that primitive types contain only data in the form of a fixed set of values (e.g., numbers ); objects can contain primitive data as well as code ( methods ) and other objects. Objects are discussed at length starting in Chapter 6. In this section, we only give a brief introduction to their usage and focus primarily on their characteristics as data types.

Objects

An object is a collection that can contain primitive or composite data, including functions and other objects. The data members of an object are called properties , and member functions are known as methods . Some readers may prefer to think of properties as the characteristics of the object and the things the object does as its methods, but the meaning is the same.

Properties are accessed by placing a period and the property name immediately following the object name . For instance, the version information of the browser is stored in the appVersion property of the Navigator object. One way of accessing this property is

 alert("Your browser version is: " + navigator.appVersion); 

the result of which in Internet Explorer 5.5 (domestic U.S.) is similar to the following:

click to expand

Methods of objects are accessed in the same way but with trailing parentheses immediately following the method name. These parentheses indicate to the interpreter that the property is a method that you want to invoke. The Window object has a method named close , which closes the current browser window:

 window.close(); 

If the method takes arguments, the arguments are included in the parentheses. We ve seen a common example of this usage, the write method of the Document object:

 document.write("This text is written to the document."); 

Built-in Objects

JavaScript provides many powerful objects for developers to use. These include browser-specific objects such as Window , which contains information and methods related to the browser window. For example, as we mentioned previously, window. open () could be used to create a window. Objects such as Document contain more objects that map to the various features and tags of the document in the window. For instance, to see the last modification date of the document, we could reference the document.lastModified property. Also available are numerous objects defined in the JavaScript language that simplify common tasks . Examples of such objects are Date , Math , and RegExp . Finally, each data type in JavaScript has a corresponding object. So there are String , Number , Boolean , Array , and even

Object objects. These objects provide the functionality commonly used to carry out data manipulation tasks for the given type. For example, we already saw that the String object provides methods like charAt() to find characters at a particular position in a string. There are so many different objects to cover that the majority of the book is spent discussing the various built-in and generated objects. However, just in case you want objects of your own, you can create those too.

Creating Objects

User-defined objects are created using the new keyword followed by the name of the object and parentheses. The reason for the parentheses is that objects are created using constructor s, methods that create a fresh instance of an object for you to use. The parentheses tell the interpreter that you want to invoke the constructor method for the given object. The following creates a brand new String object:

 var myString = new String(); 

One nice feature of objects in JavaScript is that you can add properties to them dynamically. For example, to create your own object and populate it with two text fields, you might do the following:

 var myLocation = new Object(); myLocation.city = "San Francisco"; myLocation.state = "California"; 

If you are not completely comfortable with the concept of objects from previous experience, don t worry. It will be explained at greater length in Chapter 6. The important things to understand at this point are the syntax of how properties are accessed using the . (as in myLocation.city), the notation difference between a property and a method, and that you can indeed make your own objects.

Arrays

An important wrinkle about objects in JavaScript is the composite type Array , which is an object but generally has different creation and access syntax. An array is an ordered list that can contain primitive and complex data types. Arrays are sometimes known as vectors or lists in other programming languages and are actually Array objects in JavaScript. The members of an array are called elements . Array elements are numbered starting with zero. That is, each element is assigned an index , a non-negative integer indicating its position in the array. You can think of an array as a series of boxes labeled 0, 1, 2, and so on. You can place a piece of data into a box, for example, box 5, and later retrieve that data by accessing the element at index 5. Individual array elements are accessed by following the array name with square brackets ( [ and ] ) containing the desired index. For example, to place a string in array element 5 and then retrieve it, you might write

 myArray[5] = "Hamburgers are nice, sushi is better."; var x = myArray[5]; 

Individually setting the values of an array as shown here can be rather tedious , and there are more direct ways to populate an array. Array literals are specified by a comma-separated list of values enclosed in square brackets. The following defines a new array with four numbers and one string:

 var myArray = [2, 4, 6, 8, "ten"]; 

If you want to define an array but fill it with values later, you can define an empty array in a similar manner:

 var myArray = []; 

Because arrays are really Array objects, you can use the object syntax to declare a new array:

 var myArray = new Array(); 

You can then access the array according to the syntax previously discussed.

At this point, just remember that arrays and objects really aren t that different. In fact, the main differences are that arrays are more focused on order than objects and we use different notation to access arrays. We ll talk quite a bit more about arrays in Chapter 7.

Functions

A function is another special type of JavaScript object, one that contains executable code. A function is called (or invoked ) by following the function name with parentheses. Functions can take arguments (or parameters ), pieces of data that are passed to the function when it is invoked. Arguments are given as a comma-separated list of values between the parentheses of the function call. The following function call passes two arguments, a string and a number:

 myFunction("I am an item", 67); 

The call passes myFunction two things, a string and a number, that the function will use to perform its task. You should notice the similarity with the method invocation here:

 document.write("The value of pi is: ", 3.14); 

In this case, the write method of the Document object is invoked to output a string to the current browser window. Methods and functions are indeed closely related. A simple way to think about it would be that a function appears to not be associated with an object, whereas a method is a function that is obviously attached to an object. Interestingly, once you get down into function and objects, the world gets quite complicated and you ll discover that functions are indeed first-class data types in JavaScript. This means that functions are treated just like any other non-primitive type. They can be assigned to variables , passed to other functions, and created or destroyed dynamically. We ll talk more about what all this means in Chapter 5.

The typeof Operator

If you re curious about the type of data you have, use the typeof operator to examine it. Applied to a variable or literal, it returns a string indicating the type of its argument. The list of values returned by typeof is given in Table 3-4.

Table 3-4: Values Returned by the typeof Operator

Type

Result

Undefined

undefined

Null

object

Boolean

boolean

Number

number

String

string

Object

object

Function

function




JavaScript 2.0
JavaScript: The Complete Reference, Second Edition
ISBN: 0072253576
EAN: 2147483647
Year: 2004
Pages: 209

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net