Introducing the common methods of spread operator in JavaScript
藏色散人
Release: 2021-08-11 15:50:38
forward
2473 people have browsed it
Personal understanding of...common methods
...as an expansion operation The symbol
refers to the expansion here for arrays or strings or pseudo-arrays (similar to arrays, you can use subscripts to take out each item, and there is an overall The extension of the length attribute data (hereinafter referred to as the traversable object) means to take out each item in the array and separate it with (when used, it is separated by spaces if printed separately) .
...You can expand a one-dimensional array, that is, you can only remove the outermost layer of [];
The result is the result of the array being removed from the [], not A string is neither an array nor anything else, so according to its comma-separated format, you can pass it as a formal parameter of a function, or you can wrap it with a [] outside it, and treat it as an array as the return of a function, but it cannot be returned directly. You can use console output.
The remainder operator means that I don’t know how many parameters there are in total. I use... to represent the remainder. Similarly, it only Applies to arrays.
And the result obtained by these two methods is an array, not an array with [] removed.
There are mainly two methods:
The first one: All parameters are unknown, I don’t know how many to pass.
Second type: There are one or more known parameters, and the remaining parameters are unknown.
Because it can display the traversable object by removing [] from the array, and it can be used on pseudo arrays, so it is easy to understand pseudo arrays. After use, after converting the form of the pseudo array, adding [] will become a real array, and you can call the method of the real array. => […arr.]
Shallow copy
Since it uses a loop on a traversable object, it will not change the original array, but will make a shallow clone operation, so it can be used for shallow copies of arrays. => […arr]
It can be seen from the above that when the array structure is very simple (two-dimensional), the spread operator can solve the array inside and Use concat for splicing. But when the array is multi-dimensional, it cannot solve the deeper array. It needs to use the spread operator and recursion to achieve this.
The above is the detailed content of Introducing the common methods of spread operator in JavaScript. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn