11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Arrays<br />

An important wrinkle about objects in <strong>JavaScript</strong> is the composite type Array, which is an object<br />

but generally has different creation and access syntax. An array is an ordered list that can<br />

contain primitive and complex data types. Arrays are sometimes known as vectors or lists in<br />

other programming languages and are actually Array objects in <strong>JavaScript</strong>. <strong>The</strong> members of an<br />

array are called elements. Array elements are numbered starting with zero. That is, each<br />

element is assigned an index, a non-negative integer indicating its position in the array. You<br />

can think of an array as a series of boxes labeled 0, 1, 2, and so on. You can place a piece of<br />

data into a box, for example, box 5, and later retrieve that data by accessing the element at<br />

index 5. Individual array elements are accessed by following the array name with square<br />

brackets ([ and ]) containing the desired index. For example, to place a string in array element<br />

5 and then retrieve it, you might write<br />

myArray[5] = "Hamburgers are nice, sushi is better.";<br />

var x = myArray[5];<br />

Individually setting the values of an array as shown here can be rather tedious, and there are<br />

more direct ways to populate an array. Array literals are specified by a comma-separated list of<br />

values enclosed in square brackets. <strong>The</strong> following defines a new array with four numbers and<br />

one string:<br />

var myArray = [2, 4, 6, 8, "ten"];<br />

If you want to define an array but fill it with values later, you can define an empty array in a<br />

similar manner:<br />

var myArray = [];<br />

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

var myArray = new Array();<br />

You can then access the array according to the syntax previously discussed.<br />

At this point, just remember that arrays and objects really aren‘t that different. In fact, the main<br />

differences are that arrays are more focused on order than objects and we use different<br />

notation to access arrays. We‘ll talk quite a bit more about arrays in Chapter 7.<br />

Functions<br />

A function is another special type of <strong>JavaScript</strong> object, one that contains executable code. A<br />

function is called (or invoked) by following the function name with parentheses. Functions can<br />

take arguments (or parameters), pieces of data that are passed to the function when it is<br />

invoked. Arguments are given as a comma-separated list of values between the parentheses of<br />

the function call. <strong>The</strong> following function call passes two arguments, a string and a number:<br />

myFunction("I am an item", 67);<br />

<strong>The</strong> call passes myFunction two things, a string and a number, that the function will use to<br />

perform its task. You should notice the similarity with the method invocation here:<br />

document.write("<strong>The</strong> value of pi is: ", 3.14);<br />

In this case, the write method of the Document object is invoked to output a string to the<br />

current browser window. Methods and functions are indeed closely related. A simple way to<br />

think about it would be that a function appears to not be associated with an object, whereas a<br />

method is a function that is obviously attached to an object. Interestingly, once you get down<br />

into function and objects, the world gets quite complicated and you‘ll discover that functions are<br />

indeed first-class data types in <strong>JavaScript</strong>. This means that functions are treated just like any<br />

other non-primitive type. <strong>The</strong>y can be assigned to variables, passed to other functions, and<br />

created or destroyed dynamically. We‘ll talk more about what all this means in Chapter 5.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!