1. Array.prototype.slice MethodThe slice method of an array is typically used to extract fragments from an array. However, it also has the "class array" (such as arguments and? Htmlcollection?) The ability to convert to a true array.
1 var nodesarr = array.prototype. Slice. Call (document.forms); 2 3 var argsarr = Array.prototype. Slice. Call (arguments);
I wondered why the slice method of the array had such a knack for how it was implemented in the JavaScript engine. Slice's brother method is there any such skill?
With curiosity, download Google's V8 JavaScript engine source code to the local, V8 Source: Https://github.com/v8/v8.
Look for "Array.prototype.slice" in V8-master/src/array.js:
1 functionArrayslice (start, end) {2Check_object_coercible ( This, "Array.prototype.slice");3 ...4 varresult = [];//that's the key .5 6 if(End_i < Start_i)returnresult;7 8 if(Usesparsevariant (array, Len, Is_array (array), End_i-start_i)) {9 ...TenSparseslice (Array, start_i, End_i-start_i, Len, result); One}Else { ASimpleslice (Array, start_i, End_i-start_i, Len, result); - } -... the returnResult
Then guess call "class array" Go should be the Simpleslice method, and then in the source lookup "Simpleslice", found Array.prototype.splice source also called Simpleslice method, and the result variable is also initialized to an empty array. However, to convert the "class array" to a true array using the Splice method, you must pass in the starting position parameter of 0, i.e.:
var nodesarr = Array.prototype. Splice 0);
Because it does this by composing the deleted array items into a new array. Interested in children's shoes can be seen under the Array.prototype.splice source code implementation.
In addition, slice can clone an array:
1 var arr = [1, 2, 3]; 2 var // Clonearr: [1, 2, 3]
2. Array.prototype.push method
You can combine arrays using the Push method:
1 var arr1 = [1, ' str ', {name: ' lang '}]; 2 var arr2 = [2, ' ing ']; 3 Array.prototype.push.apply (arr1, arr2); 4 // results returned: [1, "str", {name: ' lang '}, 2, "ing"]
3. Array.prototype.sort method
First on the code:
var arr = [' 1 ', ' 2 ', ' Ten ', '];arr.sort '(); // results returned: ["1", "Ten", "a", "2"]
The above results are usually not what we want, so how to sort by numerical size:
Arr.sort (function(A, b) { return A- b;}) // return results: ["1", "2", "Ten", "a")
With the sort comparer function, you can customize many comparators for personalized sorting.
4. Length Property
The length property of an array is not read-only, but it can also be written, such as using the Length property to truncate an array:
1 var arr = [1, 2, 3, 4]; 2 arr.length = 2; 3 // arr: [1, 2] 4 arr.length = 0; 5 // arr: []
At the same time, if the length property is larger, the value of the array will increase, and the undefined is used as the new element to fill.
1 var arr = []; 2 arr.length = 3; 3 // arr: [Undefined, undefined, undefined]
Well, today summed up here, already early in the morning, what new discoveries in the future append here.
Before, did not write the habit of blogging, only used to put the usual summary into the Youdao cloud notes, did not expect to write a point of view really need to spend a bit of thought, because to consider how to express, can let others understand better.
There is something wrong or understanding the wrong place, but also hope that everyone help correct it out.
JavaScript Array method Explore one or two