↑
Main Page
method
The
Array
object has a couple of methods that have equivalents in the
String
class, namely the
con-
cat()
and
slice()
methods. The
concat()
method works almost exactly the same as it does with
strings: The arguments are appended to the end of the array, and a new
Array
object (one containing
both the items in the original array and the new items) is returned as the function value. For example:
var aColors = [“red”, “green”, “blue”];
var aColors2 = arr.concat(“yellow”, “purple”);
alert(aColors2.toString()); //outputs “red,green,blue,yellow,purple”
alert(aColors.toString()); //outputs “red,green,blue”
In this example, the strings
“yellow”
and
“purple”
are added to the array using the
concat()
method. The
aColors2
array contains five values whereas the original array,
aColors
, still contains
only three. This can be proved by calling the
toString()
method on each array.
The
slice()
method is also very similar to
String
class equivalent in that it returns a new array con-
taining the specified items. Just like the
String
’s method, the
Array
’s
slice()
method can accept one
or two arguments: the starting and stopping positions of the items to extract. If only one argument is
present, the method returns all items between that position and the end of the array; if there are two
arguments, the method returns all items between the first position and second position, not including
the item in the second position. For example:
var aColors = [“red”, “green”, “blue”, “yellow”, “purple”];
var aColors2 = arr.slice(1);
var aColors3 = arr.slice(1, 4);
alert(aColors2.toString()); //outputs “green,blue,yellow,purple”
alert(aColors3.toString()); //outputs “green,blue,yellow”
Here,
aColors2
contains all the items in
arr
from position 1 on. Because the string
“green”
is in position
1, this is the first item in the new array. For
aColors3
, the
slice()
method is called with two arguments,
1 and 4. The string
“green”
is in position 1 and the string
“purple”
is in position 4, so
aColors3
con-
tains
“green”
,
“blue”
, and
“yellow”
because
slice()
only includes the item immediately before the
last position.
One of the interesting things about the ECMAScript
Array
class is that it provides methods to make an
array behave like other types of data structures. An
Array
object, for example, can act just like a stack,
which is one of a group of data structures that restrict the insertion and removal of items. A stack is
referred to as a
last-in-first-out
(LIFO) structure, meaning that the most recently added item is the first
one removed. The insertion and removal of items in a stack occur at only one point: the top of the stack.
It helps to think of a stack in literal terms, such as a stack of plates. If you want to add a plate to the stack
of plates, you place the plate on top of the stack. When an item is added to a stack data structure, it is
said to be
pushed
onto the stack; it is added at the top (Figure 3-1).
When it comes time to remove a plate for dinner, what do you do? You remove the top plate from the
stack of plates and put it on the table. Again, the stack data structure works the same way, removing
only the topmost item. When an item is removed from a stack, it is said to be
popped
from the stack
(Figure 3-2).
73
Object Basics
06_579088 ch03.qxd 3/28/05 11:36 AM Page 73
Free JavaScript Editor
Ajax Editor
©
→