Please explain the reason, especially c
伊谢尔伦2017-05-19 10:20:03
First of all, in ES6, ...
itself has the function of a structural object.
const [...a] = [1, 2, 3]
a // [1, 2, 3]
So for destructuring assignment and the one-to-one correspondence between elements, the question is broken down into
let [a, b, ...c] = [a, undefined, undefined]
过去多啦不再A梦2017-05-19 10:20:03
What could be the reason for this...
This is how destructuring assignment and aggregation operations are stipulated...
In the rvalue array of the assignment operation, there are no elements in the third position and beyond, then the c
array is of course an empty array...
迷茫2017-05-19 10:20:03
var [a,b,...c] = ['a']
//c是不定参数,b是默认参数(undefined)
console.log(a); //a
console.log(b); //undefined 解构不成功就是undefined
console.log(c); //[] 不定参数解构不成功是空数组[],它永远不可能为undefined
Array destructuring has different rules for default parameters and variable parameters
習慣沉默2017-05-19 10:20:03
let [x,y, ...c] = ['a']
等于
let x, y, c = ..c
['a'].forEach(function (item, index, array) {
if (index === 1) {
x = array[index]
}
if (index === 2) {
y = array[index]
}
if (index === 3) {
c = array[index]
}
})
// 因为只有一个值,所以就x的变化了,而y和c没有变
// 循环里面是瞎扯的,但大概是这个原理