是否可以指定一个splat应该匹配多少个元素?比如:
foo = [1, 2, 3, 4, 5, 6]
[firstThree...(3), fourth, rest...] = foo
console.log firstThree // [1, 2, 3]
console.log forth // 4
console.log rest // [5, 6]
据我所知,没有办法限制splat可以接受的参数数量。
但是你可以使用范围(在循环和推导文档中搜索range
)在你的解构赋值中获得类似的语法:
foo = [1, 2, 3, 4, 5, 6]
[firstThree, fourth, rest] = [foo[0..2], foo[3], foo[4..-1]]
firstThree
# => [1, 2, 3]
fourth
# => 4
rest
# => [5, 6]