This article brings you relevant knowledge about javascript, which mainly introduces related issues about the expansion operator. The expansion operator of S6 has a very simple syntax. It uses three The dot sign means "...". You can convert an array into a parameter sequence separated by commas. Let's take a look at it. I hope it will be helpful to everyone.
【Related recommendations: javascript video tutorial, web front-end】
ES6’s expansion operator, Its syntax is very simple, using three periods to represent "...". You can convert an array into a comma-separated sequence of parameters.
It expands the iterable object into its individual elements. The so-called iterable object is any object that can be traversed using a for of
loop, such as: array, string, Map
, Set
, DOM
nodes, etc.
1 2 3 4 |
|
1 2 3 4 5 6 7 8 |
|
In the above code, array.push(...items)
and add(...numbers)
These two lines are both function calls, and they both use the spread operator. This operator turns an array into a sequence of parameters.
1 2 3 4 |
|
If the expansion operator is followed by an empty array, it will have no effect.
1 2 |
|
There are many other uses of the spread operator...
1 |
|
But if you want to calculate the maximum value in the array, obviously the array cannot be directly used as a parameter of Math.max(), we need to expand it. Before ES6, we also needed to combine apply to process:
1 2 3 4 5 6 |
|
ES6 can be easily expanded using the spread operator (...). The above example becomes:
1 2 |
|
The expansion operator gives us a new method of merging arrays
1 2 3 4 5 |
|
Using the spread operator can easily expand the array into a parameter list
1 2 3 4 5 6 |
|
In the above code, a3
and a4
use two different methods The new arrays are merged, but their members are references to the original array members. This is a shallow copy. If the value pointed to by the reference is modified, it will be reflected in the new array synchronously.
Note: These two methods are shallow copies, so you need to pay attention when using them.
Array is a composite data type. If you copy it directly, you only copy the pointer to the underlying data structure. Not cloning a completely new array.
ES5 can only use workarounds to copy arrays.
1 2 3 4 |
|
In the above code, a1
will return a clone of the original array, and modifying a2
will not affect a1
.
The spread operator provides a simple way to copy an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Remember: the array is still obtained through the pointer, so we do not copy the array itself, we copy just a new pointer.
NodeList
The object is a collection of nodes, usually Returned by properties such asNode.childNodes
and methods such asdocument.querySelectorAll
.
像 NodeList 和 arguments 这种伪数组,类似于数组,但不是数组,没有 Array
的所有方法,例如find
、map
、filter
等,但是可以使用 forEach()
来迭代
可以通过扩展运算符将其转为数组,如下:
1 2 3 4 |
|
注意:使用扩展运算符将伪数组转换为数组有局限性,这个类数组必须得有默认的迭代器且伪可遍历的
扩展运算符可以与解构赋值结合起来,用于生成数组
1 2 3 4 |
|
下面是另外一些例子:
1 2 3 4 5 6 7 8 9 |
|
注意:如果将扩展运算符用于数组赋值,只能放在参数的最后一位,否则会报错。
1 2 3 4 |
|
ES6的扩展语法可以很简单的把一个字符串分割为单独字符的数组:
1 2 |
|
扩展运算符内部调用的是数据结构的 Iterator 接口,因此只要具有 Iterator 接口的对象,都可以使用扩展运算符,比如 Map 结构。
1 2 3 4 5 6 |
|
Generator 函数运行后,返回一个遍历器对象,因此也可以使用扩展运算符。
1 2 3 4 5 6 |
|
上面代码中,变量go
是一个 Generator 函数,执行后返回的是一个遍历器对象,对这个遍历器对象执行扩展运算符,就会将内部遍历得到的值,转为一个数组。
如果对没有 Iterator 接口的对象,使用扩展运算符,将会报错。
1 2 |
|
【相关推荐:javascript视频教程、web前端】
The above is the detailed content of Let's talk about the spread operator in ES6. For more information, please follow other related articles on the PHP Chinese website!