Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

This method is more complicated than the previous ones so we shall explain it in depth.

At first, two arrays are declared. Although it is possible to calculate their size, it is completely unnecessary because they will accommodate the proper size as we expand them. The first loop in the function is:

for (i = 0; i < length; ++i) {
  ar1[i] = this[i + offset]
}

This loop assigns all elements that are to be removed to the array ar1, according to the values of the offset and length parameters. The number of times the loop iterates is exactly the value of length, the number of elements specified to be removed. The array ar1 is populated from the beginning, 0. It is assigned the elements of the calling array (this), starting from offset, because only elements following offset (exactly length of them) are to be removed. When the loop terminates, the array ar1 already consists of elements that should be removed from the calling array. Notice that this array is returned at the end of the function.

Now take a look at the second loop:

for (i = 0; i < this.length – (offset + length); ++i) {
  ar2[i] = this[i + offset + length]
}

This loop assigns all elements following the removed ones to the array ar2. The loop terminates after it has completed assigning all these elements. this.length – (offset + length) is equal to the number of elements following the removed one. The subscript of the first element following the removed set is offset + length. Subtracting this number from the full length of the calling array gives the number of elements following the removed set.

The next loop is as follows:

for (i = 0; splice.arguments[i + 2] != null; ++i) {
  args[i] = splice.arguments[i + 2] // second argument
}

The preceding loop assigns all items of list (the third parameter of the method) to the array args. These items are all arguments handed over to the method except for the first two. We use the condition splice.arguments[i + 2] != null to terminate the loop due to some unexpected behavior of the arguments.length property on some platforms. That is, when the function comes across the first null element of the splice.arguments (see Chapter 9, Functions and Variable Scope), it terminates. This action is based on the fact that the method will never be called to replace an element of the calling array by a null one. This concept is explained later in this chapter in depth. The expression i + 2 is used because the needed elements of the arguments array are only the third one (subscript == 2) on. However, the args array created in the function should store the elements starting at subscript 0, so i is used as the subscript of that array.

We now have four arrays:

  • this (the calling array)
  • args
  • ar2
  • ar1

Keep in mind that the splice() method needs to modify the calling script. It concatenates the beginning of this array (up to the first removed, exclusively) with args and ar2, in this order. The last portion of the script does just that:

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

At first, j is set to offset, the first value of the array this that should be modified (all the existing elements before should remain at the beginning of the array, as they were before calling the method). The following loops assign elements from the other arrays, args and ar2, respectively, to the calling array (this). The variable j is important because it holds the subscript of the current element of the calling array throughout both loops. At the end, the length of the calling array (now modified) is set to j, so if there were more elements in the original array, they are chopped off.

The last statement of the splice() function returns the removed elements in the form of an array, ar1.

After this long explanation, some examples will surely clear up the method:

var cols = new Array("red", "orange", "yellow", "green",
  "blue", "purple")

var newCols = cols.splice(2, 3, "brown", "black")
document.write(newCols.join(" ") + "<BR>")
document.write(cols.join(" ") + "<BR>")
cols = new Array("red", "orange", "yellow", "green",
  "blue", "purple")

newCols = cols.splice(0, 1, "brown", "black")
document.write(newCols.join(" ") + "<BR>")
document.write(cols.join(" ") + "<BR>")

cols = new Array("red", "orange", "yellow", "green", "blue", "purple")

newCols = cols.splice(3, 0, "brown", "black")
document.write(newCols.join(" ") + "<BR>")
document.write(cols.join(" "))

The output of this script segment is:

yellow green blue
red orange brown black purple
red
brown black orange yellow green blue purple
[blank line]
red orange yellow brown black green blue purple

The function will not work if you try to remove elements of the array without inserting new ones.

split()

The split() method does the opposite of join(). It splits up a string (the object) by some delimiter (space by default) and returns an array. To be accurate, this is a method of the String object, not the Array one, but because it is closely related to arrays, we chose to discuss it in this section. If the delimiter is not supplied or is not found in the entire string, it returns the string itself. This method is built in so you just have to call it. The general format is:

stringName.split(delimiter)

Here are some examples:

var line1 = "a b c d e f"
var ar1 = new Array()

ar1 = line1.split(" ")
document.write(ar1.join(", ")) // a, b, c, d, e, f
document.write("<BR>")
ar2 = line1.split(";")
document.write(ar2) // a b c d e f
document.write("<BR>")
ar3 = line1.split()
document.write(ar3) // a b c d e f
document.write("<BR>")
ar4 = line1.split("")
document.write(ar4) // [infinite function!]

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us