In JavaScript, arrays can be created using the Array constructor, or quickly created using [], which is also the preferred method. Array is the prototype inherited from Object, and it has no special return value for typeof, it only returns 'object'.
1. Array.prototype.slice method
The array's slice method is typically used to extract a slice from an array. However, it also has the ability to convert "array-like" objects (such as arguments and HTMLCollection) into real arrays.
I’m just curious why the array’s slice method has such a capability, and how is it implemented in the javascript engine? Does the sibling method of slice have such ability?
With curiosity, download Google’s V8 javascript engine source code locally. The download address of V8 source code: https://github.com/v8/v8.
Look for "Array.prototype.slice" in v8-master/src/array.js:
Then I guessed that the SimpleSlice method should be used to call the "class array", and then searched for "SimpleSlice" in the source code, and found that the SimpleSlice method was also called in the Array.prototype.splice source code, and the result variable was also initialized to an empty array. However, if you want to use the splice method to convert the "array-like" into a real array, you must pass in the starting position parameter as 0, that is:
Because its implementation principle is to form a new array from the deleted array items. Interested children can take a look at the source code implementation of Array.prototype.splice.
In addition, slice can also clone an array:
2. Array.prototype.push method
Arrays can be merged using the push method:
3. Array.prototype.sort method
First enter the code:
The above results are usually not what we want, so how to sort by numerical value:
With the sorting comparator function, you can customize many comparators to achieve personalized sorting.
4. length attribute
The length attribute of the array is not read-only, which means it can also be written. For example, use the length attribute to truncate the array:
At the same time, if the length attribute is made larger, the length value of the array will increase, and undefined will be used as the new element to fill.
Okay, that’s all for today. It’s already early in the morning. I’ll append here if I find any new discoveries in the future.
Before, I didn’t have the habit of blogging. I was only used to putting my usual summaries in Youdao Cloud Notes. I didn’t expect that it takes a lot of thought to write out my opinions, because I have to consider how to express them so that others can better understand.
If there is any incorrect expression or misunderstanding, please help me correct it.
Also attached are 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 the specified delimiter.
pop() removes 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() reverses the order of elements in an array.
shift() deletes and returns the first element of the array
slice() returns selected elements from an existing array
sort() sorts the elements of the array
splice() removes elements and adds new elements to the array.
toSource() returns the source code of the object
toString() converts the array to a string and returns the result.
toLocaleString() converts the array to a local array and returns the result.
unshift() adds one or more elements to the beginning of the array and returns the new length.
valueOf() returns the original value of the array object