E.2 arguments[] (Core JavaScript)
Syntax:
arguments
arguments[n ]
The arguments object is an array-like object of the Argume nts type. It is defined only
within a body of a function and holds a ll the values that were passed as arguments to
the function.
Object Instance Creation
Each time a function is called, an Arguments object is created, which holds all the
arguments p assed to the function. Technically, arguments is a local variable of the
function, which is automatically de clared and initialized to refer to the created Argu-
ments object.
Properties
length
Syntax:
arguments.length
This property defines the num ber of arguments passed to the function. It is important
to note that this is the number of arguments actu ally passed and not the number ex-
pected. The property enables a programmer to check wh ether all the parameters have
been passed or to fetch additional, unnam ed arguments of the function.
There’s an examp le of using arguments on page 194.
E.3 Array (Core JavaScript)
The Array object acts as a constructor for arrays and therefore defines an object
class. An array is a list-like object with pro totyped methods suited for list manip ula-
tions. Basically, you can think of an a rray as a collection of dierent objects—called
elements—which you can traverse, add, delete, search, so rt, and so on.
Object Instance Creation
Array() //Constructor
Syntax:
new Array(element1 , element2, ..., elementN )
374 JavaScript Reference
JavaScript Reference
new Array(arrayLength )
new Array()
Creates, initializes and returns a new array. The create d array is initialized with the
values passed as arguments to a c onstructor (element1 , element2 , ..., elementN ) . If
only a single numeric argument is passed to the Array() constructor (arrayLength ),
which is an integer between 0 and 2
321
inclusive, then the constructor creates an
array with the given number of u ndefined elements. When the Array() constructor
is invoked without any arguments, it returns an empty array, whose length property
has a value o f 0.
Examples:
var a = new Array(42, 4); //An array with two elements: 42 and 4
var a = new Array(); //An empty array
var a = new Array(42); //An array with 42 undefined elements
var a = new Array("q"); //An array with a single element "q"
[] //Literal syntax
Syntax:
[element1, element2 , ..., elementN ]
An alternative way of creating an array is to place a comma-separated list of expres-
sions inside square brackets. The values of the supplied exp ressions become the array
elements. Note that a single numeric value is treated as a single element with which
to initialize the array and not as the number of undefined elements like it is the case
with the Array() constructor.
Examples:
var a = [42, 4]; //An array with two elements: 42 and 4
var b = []; //An empty array
var c = [42]; //An array with a single element 42
var d = [a[1], b.length]; //An array with two elements: 4 and 0
Properties
length
Syntax:
array .length
E.3. Array (Core JavaScript) 375
The length property is an integer whose value is one big ger than the index of the last
element of array . If array has contiguous elements, then le ngth tells the number
of elements in array . You can also directly change th e value of the length p roperty,
which truncates or lengthens array .
Examples:
var a = [5, 2, 9];
console.log(a.length); //The length of a is 3
a.length = 2; //Truncates a to two elements
console.log(a[2]); //The third element of a is now undefined
Methods
indexOf()
Syntax:
array .indexOf(searchElement )
array .indexOf(searchElement , searchFrom )
The indexOf() method searches array for the first element that is equal to search
Element and returns the index of the f ound element. By default, the sear ch begins
at the first element ( w ith the index of 0), unless the optional searchFrom argument
specifies a dierent start ind ex. If no match ing element is fo und, then indexOf()
returns -1. Note that the metho d uses the === operator for testing equality.
Examples:
var arr = [5, 40, "9", 40];
arr.indexOf(40) //Returns 1
arr.indexOf(40, 2) //Returns 3
arr.indexOf(9) //Returns -1
arr.indexOf("9") //Returns 2
arr.indexOf(5, 1) //Returns -1
lastIndexOf()
Syntax:
array .lastIndexOf(searchElement )
array .lastIndexOf(searchElement , searchFrom )
The la stIndexOf() meth od searches array back ward s for the first elem ent tha t
equals searchElement and returns the index of the found element. By de fault, the
search begins at the last element (with the index of array .length - 1), u nless the
376 JavaScript Reference
JavaScript Reference
optional searchFrom argum ent specifies a dierent start index. I f no matching ele-
ment is found, then indexOf() returns -1. Note that the method uses the === op erator
for testing equality.
Examples:
var arr = [5, 40, "9", 40];
arr.lastIndexOf(40) //Returns 3
arr.lastIndexOf(40, 2) //Returns 1
arr.lastIndexOf(5, 1) //Returns 0
arr.lastIndexOf("9", 1) //Returns -1
pop()
Syntax:
array .pop()
The pop() method returns the value of the last ele ment in array and removes that
element from the array. In order to reflect the change in the size of the array, the
length property is decremented accordingly. If pop() is invoked on an empty a rray,
then it returns the undefined value a nd the array is not changed.
Examples:
var arr = [2, 4, 11];
arr.pop(); //Returns 11, arr is now [2, 4]
arr.pop(); //Returns 4, arr is now [2]
arr.pop(); //Returns 2, arr is now []
arr.pop(); //Returns undefined, arr is still []
push()
Syntax:
array .push(element1, element2, ..., elementN )
This method appends one o r more elements (element1 , element2 , ..., elementN )
to the end of array . The method also increments the length property to reflect the
new size of the array and returns it.
Examples:
var arr = [];
arr.push(2, 3, 5); //Returns 3, arr is now [2, 3, 5]
arr.push(7); //Returns 4, arr is now [2, 3, 5, 7]
arr.push(11); //Returns 5, arr is now [2, 3, 5, 7, 11]
E.3. Array (Core JavaScript) 377
reverse()
Syntax:
array .reverse()
The reverse() method reverses the order of the array elements, so that the first ele-
ment o f the array becomes the la st and vice versa. The rearrangement of the elements
is done in place, i.e., the elements of the array on which the method is invoked are re-
ordered without creating a new array. The method returns the r eference to the reversed
array, so that the method cha ining is possible.
Example:
var arr = [1, 1, 2, 3, 5, 8];
arr.reverse(); //arr is now [8, 5, 3, 2, 1, 1]
sort()
Syntax:
array .sort()
array .sort(compareFunction )
The sort() meth od sorts the elements of an array, whic h is done in place (i.e., no
copy of the original array is made ), and return s the reference to the sorted array so that
the method chaining is possible. If no additional argument is supplied , then the array
elements are sorted alphabetically (more specifically, in an ascending order according
to the character codes determined by the character encoding).
Because the array elements are sorte d alphabetically, numbers are not arranged as y ou
might expect. For example, 2 comes before 42 in a numeric sort, but since numbers
are converted to strings before sor t is applied, "42" comes before "2".
If you want to use some other criteria to sort elements, then you can supply a custom
compariso n function (compareFunction ). Th is function takes two arguments and
must return one of the following values:
A value less tha n zero if the first argument should app ear before the second one
in the sorted array.
A value greater than zero if the second argument should appear before the first
one in the sorted array.
Zero if both arguments are co nsidered equal in the scope of this sort and their
order doesn’t matter.
If there are any undefined elements, they always come at the end of the sorted array.
This is not possible to change even if you pr ovide your own comparison fu nction
because undefined values are never passed to the compareFunction function.
378 JavaScript Reference
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.57.164