|
An important concept used by this method is short-circuit evaluation. If JavaScript evaluates the first conditional expression (typeof this[i] == "string") to false, the final expression (typeof this[i] == "string" && this[i].indexOf(str) != –1) is obviously false, so the second expression (this[i].indexOf(str) != –1) is not even evaluated. This is critical because the indexOf() method works only with strings, and generates an error otherwise. However, if the current element (this[i]) is not a string, JavaScript does not evaluate the second expression due to short-circuit evaluation. join()The join() method is a built-in one in JavaScript. It is equivalent to the same function in Perl. It joins the elements of an array into a single string and separates each element with a given delimiter. This method is exactly like the getList method we created earlier, so we will use it from this point on instead. Its general syntax is:
arrayInstance.join(delimiter)
The delimiter is automatically cast to a string if it is not already one. Here is an example using this method:
var line = new Array("a", "b", "c", "d", "e") document.write(line.join(" : ")) var str = line.join(", ") document.write("<BR>" + str)
Its output is:
a : b : c : d : e a, b, c, d, e
pop()The pop method pops off the last element of an array and returns it. The array size is automatically decreased by one. The general format is:
arrayInstance.pop()
This method is not built in, so we need to define it. The following script defines the method as a prototype of the Array object, and demonstrates its use:
function pop() { var lastElement = this[this.length – 1] this.length-- return lastElement } Array.prototype.pop = pop // EXAMPLE //////////////////////////////////////////// var names = new Array("Tom", "Mark", "Bart", "John") var last = names.pop() document.write(last + "<BR>") document.write(names.join(" ") + "<BR>") document.write(names.length)
The script’s output by rows is:
John Tom Mark Bart 3
push()The push method, not supported by JavaScript, pushes values onto the end of an array, increasing its length. Here is the method declared as a prototype of the Array object type:
function push() { var sub = this.length for (var i = 0; i < push.arguments.length; ++i) { this[sub] = push.arguments[i] sub++ } } Array.prototype.push = push // EXAMPLE //////////////////////////////////////////// var names = new Array("Tom", "Mark", "Bart", "John") names.push("Jim", "Richard", "Tim") document.write(names.join(" "))
The output is:
Tom Mark Bart John Jim Richard Tim
The general syntax is:
arrayInstance.push(list)
In some situations, this method returns wrong results. The reason is explained in Chapter 9, Functions and Variable Scope. It is also explained later in this chapter in depth. reverse()The reverse method transposes the elements of the calling array object. If it was descending, now it is ascending, etc. The last element becomes the first one, and vice versa. This method is a built-in one. The general syntax is:
arrayInstance.reverse()
Here are some examples:
var names = new Array("Tom", "Mark", "Bart", "John") var colors = new Array("red", "orange", "yellow", "green", "blue", "purple") document.write("<B>original names: </B>" + names.join(" ") + "<BR>") names.reverse() document.write("<B>reversed names: </B>" + names.join(" ") + "<BR>") document.write("<B>original colors: </B>" + colors.join(" ") + "<BR>") colors.reverse() document.write("<B>reversed colors: </B>" + colors.join(" ") + "<BR>")
The corresponding output is:
original names: Tom Mark Bart John reversed names: John Bart Mark Tom original colors: red orange yellow green blue purple reversed colors: purple blue green yellow orange red
shift()The shift method is not defined in JavaScript so we will have to create it. It shifts off and returns the first element of an array, decreasing the size of an array by one element. Here is the method along with an example:
function shift(str) { var val = this[0] for (var i = 1; i < this.length; ++i) { this[i – 1] = this[i] } this.length-- return val } Array.prototype.shift = shift // EXAMPLE //////////////////////////////////////////// var line = new Array("aaa", "bbb", "ccc", "ddd", "eee") document.write(line.shift() + "<BR>") document.write(line.join(" "))
The output is:
aaa bbb ccc ddd eee
The general format of this method call (after defining its prototype) is:
arrayInstance.shift()
splice()Another method not featured yet by JavaScript is splice. It removes and replaces elements in an array. Its general syntax is:
arrayInstance.splice(offset, length, list)
offset is the starting position where elements are to be removed from. The length is the number of elements to be removed, starting at offset. The list parameter consists of new elements that are designated to replace the removed ones. Here is the method:
function splice(offset, length) { var ar1 = new Array() var ar2 = new Array() for (i = 0; i < length; ++i) { ar1[i] = this[i + offset] } for (i = 0; i < this.length – (offset + length); ++i) { ar2[i] = this[i + offset + length] } var args = new Array() for (i = 0; splice.arguments[i + 2] != null; ++i) { args[i] = splice.arguments[i + 2] // second argument } j = offset for (i = 0; i < args.length; ++i) { this[j] = args[i] j++ } for (i = 0; i < ar2.length; ++i) { this[j] = ar2[i] j++ } this.length = j // notice that j is one more than subscript of last element return ar1 } Array.prototype.splice = splice
|
|||||||||||||||||||||||
With any suggestions or questions please feel free to contact us |