Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

The expected output is:

aaa bbb ccc ddd eee

The arguments Array

The functionName.arguments array holds the arguments by order that were passed to the function functionName when that function was last called. Here is an example:

function func() {
  document.write(func.arguments[2])
}
func(1, 2, 3, 4, 5)
document.write("<BR>")
func()

The output of this script is:

3
3

The second 3 is not an argument passed to the function during the second call, but an argument passed to the function during the first call. It remains an element in the array provided that no argument of a later call replaces it. You can therefore conclude that the functionName.arguments array has a permanent storage class.

Let’s say you want to create a function that accepts string arguments and prints them with a line break after each one. The intuitive function would be:

function printList() {
  for (var i = 0; i < printList.arguments.length; ++i) {
 document.write(printList.arguments[i] + "<BR>")
  }
}

You can use this function in the following form:

printList("message 1", "message 2", "message 3", "message 4")

And the output is:

message 1
message 2
message 3
message 4

You can also use the function twice in a row as in:

printList("message 1", "message 2", "message 3", "message 4")
printList("message 5", "message 6", "message 7", "message 8")


Figure 13-3.  A one-dimensional array.

Once again, the expected output is:

message 1
message 2
message 3
message 4
message 5
message 6
message 7
message 8


Figure 13-4.  A two-dimensional array.

Now let’s change the function to the following code:

function printList() {
  document.write("There are " + printList.arguments.length +
   " arguments.")
  document.write("<BR>")
  document.write("The fourth argument is " + printList.arguments[3] +
   ".")
  document.write("<BR>")
  for (var i = 0; i < printList.arguments.length; ++i) {
 document.write(printList.arguments[i] + "<BR>")
  }
}

The function now displays the number of arguments according to the functionName.arguments.length property. It also displays the fourth argument, whether or not there are four arguments. Consider the following statements:

printList("message 1", "message 2", "message 3", "message 4")
printList("message 5", "message 6", "message 7")

The output is:

There are 4 arguments.
The fourth argument is message 4.
message 1
message 2
message 3
message 4

There are 3 arguments.
The fourth argument is message 4. (!!!)
message 5
message 6
message 7

Notice that the function prints the fourth argument passed to the function in each call. However, the second call provides only three arguments! The arguments.length property holds the correct value, 3. The fourth argument is actually a leftover from the first function call.

As you can see, the arguments array does not follow the rules of arrays that are instances of the Array object. The following script demonstrates the difference:

var ar = new Array("message 1", "message 2", "message 3")
document.write("The array is: " + ar.join(", ") + "<BR>")
document.write("There are " + ar.length + " elements.<BR>")
document.write("The third element is " + ar[2] + ".<BR>")
ar.length = 2
document.write("There are " + ar.length + " elements.<BR>")
document.write("The third element is " + ar[2] + ".<BR>")

The output of this script is:

The array is: message 1, message 2, message 3
There are 3 elements.
The third element is message 3.
There are 2 elements.
The third element is null.

The simple rule regarding arrays is that the last element’s subscript is arrayName.length – 1. When you reduce the size of an array by assigning a smaller number to the length property, all elements that were in the range of the array and are now out of range are lost. If you try referring to them, you see that they are null. The functionName.arguments array is different in that its last element has nothing to do with its length.


Note:  At this stage it is not clear if this is a bug or intentional. Note that this symptom appears in all versions of Navigator 3.0 and MSIE 3.0. To find out if this is a bug you can run the scripts in this section under the latest release of Netscape Navigator or Microsoft Internet Explorer (or any other JavaScript-enabled browser).


Multidimensional Arrays

JavaScript does not feature multidimensional arrays, but they are fairly easy to create via a constructor function.

Up to this point we discussed one-dimensional arrays (1D arrays). Such arrays consist of elements gathered in a row-like structure. They are also known as horizontal arrays. The basic structure of a one-dimensional array is outlined in the following illustration:

Let’s say you want to store a runner’s record times in different years and for different distances. If the array is called curDis, the record time of 1994 could be put in curDis[0], the record time of 1995 in curDis[1], the record time of 1996 in curDis[2], and so forth. However, you probably noticed that we can only store the record time for a single distance, such as a 400-meter run. It would be easy to store the record times of various runs in this format using a table or spreadsheet-like array. Such arrays are known as two-dimensional (2D) arrays. Figure 13-4 outlines the basic structure of a two-dimensional array:

JavaScript does not have a built-in two-dimensional array object type. Take a look at the subscripts of the elements. The two-dimensional array is constructed from horizontal arrays (horizontal in the illustration as well) which form an array of their own. That is, the general structure is a regular array of arrays. The main array in the illustration is the vertical one; its subscripts are specified to the left of the arrows.

Note that the dimensions of a multidimensional array do not have to be equal.

The standard reference to elements of a two-dimensional array (2DA) is:

ar[1][5]
ar[8][0]
ar[3][3]

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us