-
Book Overview & Buying
-
Table Of Contents
Object-Oriented JavaScript
The Array constructor creates array objects:
>>> var a = new Array(1,2,3);
This is the same as the array literal:
>>> var a = [1,2,3]; //recommended
When you pass only one numeric value to the Array constructor, it's assumed to be the array length. An array with this length will be created, and filled with undefined elements.
>>> var a = new Array(3); >>> a.length
3
>>> a
[undefined, undefined, undefined]
This can sometimes lead to some unexpected behavior. For example, the following use of the array literal is valid:
>>> var a = [3.14] >>> a
[3.14]
However, passing the floating-point number to the Array constructor is an error:
>>> var a = new Array(3.14)
invalid array length
|
Property/Method |
Description |
|---|---|
|
|
The number of elements in the array. >>> [1,2,3,4].length 4 |
|
|
Merges arrays together. >>> [1,2].concat([10,20], [300,400]) [1, 2, 10, 20, 300, 400] |
|
|
Turns an array into a string. The separator parameter is a string and the default value is a comma. >>> [1,2,3].join() "1,2,3" >>> [1,2,3].join('|')
"1|2|3" >>> [1,2,3].join(' is less than ')
"1 is less than 2 is less than 3" |
|
|
Removes the last element of the array and returns it. >>> var a = ['une', 'deux', 'trois']; >>> a.pop() "trois" >>> a ["une", "deux"] |
|
|
Appends elements to the end of the array and returns the length of the modified array. >>> var a = [];
>>> a.push('zig', 'zag', 'zebra','zoo');
4 |
|
|
Reverses the elements of the array and returns the modified array. >>> var a = [1,2,3]; >>> a.reverse() [3, 2, 1] >>> a [3, 2, 1] |
|
|
Like >>> var a = [1,2,3]; >>> a.shift(); 1 >>> a [2, 3] |
|
|
Extracts a piece of the array without modifying the source array. >>> var a = ['apple', 'banana','js', 'css', 'orange']; >>> a.slice(2,4) ["js", "css"] >>> a ["apple", "banana", "js", "css", "orange"] |
|
|
Sorts an array. Optionally accepts a callback function for custom sorting. The callback function receives two array elements as arguments and should return 0 if they are equal, 1 if the first is greater and -1 if the second is greater. An example of a custom sorting function that does a proper numeric sort (since the default is character sorting): function customSort(a, b){
if (a > b) return 1;
if (a < b) return -1;
return 0;
}
Example use of >>> var a = [101, 99, 1, 5]; >>> a.sort(); [1, 101, 5, 99] >>> a.sort(customSort); [1, 5, 99, 101] >>> [7,6,5,9].sort(customSort); [5, 6, 7, 9] |
|
|
This is probably the most powerful of the array functions. It can remove and add elements at the same time. The first parameter is where to start removing, the second is how many items to remove and the rest of the parameters are new elements to be inserted in the place of the removed ones. >>> var a = ['apple', 'banana','js', 'css', 'orange']; >>> a.splice(2, 2, 'pear', 'pineapple'); ["js", "css"] >>> a ["apple", "banana", "pear", "pineapple", "orange"] |
|
|
Like >>> var a = [1,2,3];
>>> a.unshift('one', 'two');
5 >>> a ["one", "two", 1, 2, 3] |
Change the font size
Change margin width
Change background colour