Home > Articles > Open Source > Ajax & JavaScript

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Properties

index

JavaScript 1.2+, JScript 5.5

Nav4+, IE4+

Syntax

var x = arrayObj.index

The index property of arrays reflects a number (which I will call n) such that (n–1) is the character where the first match starts. If myArray is the result of a string's match() method call, myArray.input.charAt(myArray.index) is the first character of the first match.

The index property only applies to arrays generated by a match() method call of a string or an exec() method call of a regular expression. (See Chapters 4, "String()," and 9, "RegExp()," for more information.)

input

JavaScript 1.2+, JScript 5.5

Nav4+, IE4+

Syntax

var x = arrayObj.input

The input property of arrays reflects the string against which the match() method call executed. If myArray is the result of a String().match() method call, myArray.input.charAt(myArray.index) is the first character of the first match.

This property only applies to arrays generated by a match() method call of a string or an exec() method call of a regular expression. (See Chapters 4, "String()," and 9, "RegExp()," for more information.)

length

Nav3+, IE4+

Syntax

[var x =] arrayObj.length [= newLength]

The length property of arrays typically indicates the number of elements in the array's element list. This is a property JavaScript updates any time the array changes.

If you reduce the length value, JavaScript crops the array until the actual length matches the length you set. If you increase it, you add more undefined elements to the array. But you can do the same by simply assigning an object to the last element you want to define —everything between the last defined value and your new defined value exists as undefined.

myArray = ["red", "green","blue"]
myArray.length = 2
alert(myArray) // returns Array() object containing "red", "green"

However, the length property is more valuable when you retrieve its value than when you set it. The two most common uses are to go through each element in an array's element list, and to add new elements to the end of the element list.

The first uses a for statement loop:

for (var loop = 0; loop < myArray.length; loop++) { /* ... */ }

The second simply assigns a value to the element index just past the last element in the array:

myArray[myArray.length] = new Object()

Setting the length property to less than zero is forbidden; it actually caused a crash in Netscape 4 during one of my tests. Likewise, the length property must be an integer; decimal values are not allowed.

  • + Share This
  • 🔖 Save To Your Account