es6之扩展运算符 三个点(…)

对象中的扩展运算符(...)用于取出参数对象中的所有可遍历属性,拷贝到当前对象之中。

注意:如果将扩展运算符用于数组赋值,只能放在参数的最后一位,否则会报错。

const [...rest, last] = [1, 2, 3, 4, 5];
// 报错
const [first, ...rest, last] = [1, 2, 3, 4, 5];
// 报错

扩展运算符还可以将字符串转为真正的数组

[...'hello']
// [ "h", "e", "l", "l", "o" ]

猜你喜欢

转载自www.cnblogs.com/shirliey/p/10650550.html