In JavaScript, arrays can be created using the Array constructor, or quickly created using [], which is also the preferred method. The array is a prototype inherited from the Object, and it does not have a special return value for typeof, it only returns 'object'.
1. Array.prototype.slice method
The slice method of an array is usually used to extract fragments from an array. However, it also has the ability to convert "class arrays" (such as arguments and HTMLCollection) into real arrays.
The code copy is as follows:
var nodesArr = Array.prototype.slice.call(document.forms);
var argsArr = Array.prototype.slice.call(arguments);
I was curious why the slice method of arrays has such a skill, and how is it implemented in the javascript engine? Does the brotherly method of slice have such skills?
With curiosity, download Google's V8 javascript engine source code to the local area. The download address of the V8 source code is: https://github.com/v8/v8.
Find "Array.prototype.slice" in v8-master/src/array.js:
The code copy is as follows:
function ArraySlice(start, end) {
CHECK_OBJECT_COERCIBLE(this, "Array.prototype.slice");
...
var result = []; // This sentence is the key
if (end_i < start_i) return result;
if (UseSparseVariant(array, len, IS_ARRAY(array), end_i - start_i)) {
...
SparseSlice(array, start_i, end_i - start_i, len, result);
} else {
SimpleSlice(array, start_i, end_i - start_i, len, result);
}
...
return result;
Then I guess that the SimpleSlice method that calls "class array" should be used, and then search for "SimpleSlice" in the source code and found that the SimpleSlice method is also called in the source code of Array.prototype.splice, and the result variable is also initialized to an empty array. However, if you want to use the splice method to convert "class array" into a real array, you must pass the starting position parameter to 0, that is:
The code copy is as follows:
var nodesArr = Array.prototype.splice.call(document.forms, 0);
Because its implementation principle is to form a new array by deleted array items. If you are interested, you can check the source code implementation of Array.prototype.splice.
In addition, slice can also clone an array:
The code copy is as follows:
var arr = [1, 2, 3];
var cloneArr = arr.slice(); // cloneArr: [1, 2, 3]
2. Array.prototype.push method
Use the push method to merge arrays:
The code copy is as follows:
var arr1 = [1, 'str', {name: 'lang'}];
var arr2 = [2, 'ing'];
Array.prototype.push.apply(arr1, arr2);
// Return result: [1, "str", {name: 'lang'}, 2, "ing"]
3. Array.prototype.sort method
First upload the code:
The code copy is as follows:
var arr = ['1', '2', '10', '12'];
arr.sort();
// Return result: ["1", "10", "12", "2"]
The above results are usually not what we want, so how do we sort by numerical size:
The code copy is as follows:
arr.sort(function(a, b) {
return a - b;
})
// Return result: ["1", "2", "10", "12"]
With the sorting comparator function, you can customize many comparators to achieve personalized sorting.
4. length attribute
The length attribute of an array is not read-only, which means it can be written. For example, use the length attribute to truncate the array:
The code copy is as follows:
var arr = [1, 2, 3, 4];
arr.length = 2;
// arr: [1, 2]
arr.length = 0;
// arr: []
At the same time, if the length attribute is increased, the length value of the array will increase, and undefined is used as a new element to fill.
The code copy is as follows:
var arr = [];
arr.length = 3;
// arr: [undefined, undefined, undefined]
Okay, let’s finish this day. It’s already early morning. If you have any new discoveries in the future, you will append it here.
Before, I didn’t have the habit of writing blogs, but I was only used to putting my usual summary into Youdaoyun’s notes. I didn’t expect that it would take some thought to write out my views, because I had to consider how to express it so that others could better understand it.
If you have any wrong expression or wrong understanding, I hope everyone can help you correct me.
Also attached some commonly used javascript array methods
concat() concatenates two or more arrays and returns the result.
join() puts all elements of the array into a string. Elements are separated by specified delimiters.
pop() deletes and returns the last element of the array
push() adds one or more elements to the end of the array and returns the new length.
reverse() reverse() reverses the order of elements in the array.
shift() deletes and returns the first element of the array
slice() returns the selected element from an existing array
sort() sorts the elements of the array
splice() deletes the element and adds new elements to the array.
toSource() returns the source code of the object
toString() converts the array into a string and returns the result.
toLocaleString() converts the array into a local array and returns the result.
unshift() adds one or more elements to the beginning of the array and returns a new length.
valueOf() returns the original value of the array object