First see the explanation on www.w3school.cn: http://www.w3school.com.cn/jsref/jsref_slice_array.asp
Definition and usage
The slice () method returns the selected element from an existing array.
Grammar
Arrayobject.slice (Start,end)
| Parameters |
Description |
| Start |
Necessary. Specify where to start the selection. If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on. |
| End |
Optional. Specifies where to end the selection. The parameter is the array subscript at the end of the array fragment. If this parameter is not specified, then the segmented array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the element starting from the end of the array. |
return value
Returns a new array containing elements from start to end (excluding the element) from the Arrayobject.
Description
Note that the method does not modify the array, but instead returns a subarray. If you want to delete an element from an array, you should use Method Array.splice ().
Hints and Notes
Note: You can use negative values to select elements from the end of an array.
Note: If end is not specified, then the slice () method selects all elements from start to the end of the array.
Example 1
In this example, we will create a new array and then display the elements selected from it:
<script type= "Text/javascript" >var arr = new Array (3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" Document.writ E (arr + "<br/>") document.write ( arr.slice(1) + "<br/>") document.write (arr) </script>
Output:
George,john,thomasjohn,thomasgeorge,john,thomas
Example 2
In this example, we will create a new array and then display the elements selected from it:
<script type= "Text/javascript" >var arr = new Array (6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "Jame S "arr[4] =" Adrew "arr[5] =" Martin "document.write (arr +" <br/> ") document.write ( arr.slice(2,4) +" <br/> ") document.write (arr) </script>
Output:
George,john,thomas,james,adrew,martinthomas,jamesgeorge,john,thomas,james,adrew,martin
This is the most basic usage.
Today, when we look at the jquery source code, there is a snippet:
Convert an array.
Toarray:function () {return core_slice.call (this);},
Among them: Core_slice=[].slice;
By querying the information to understand:
In addition to normal usage, slice is often used to convert Array-like objects to true array. This usage is often used in a number of frameworks.
Array.prototype.slice.call (arguments); //Convert the parameter to a true array.
Because arguments is not a real array, although arguments has the length property, but there is no slice method, so, Array.prototype.slice () execution, Array.prototype has been changed to arguments by call because it satisfies the conditions of the slice execution (with the length attribute).
The use of Array.prototype.slice in JavaScript.