Heim > Fragen und Antworten > Hauptteil
Bitte erläutern Sie den Grund, insbesondere c
伊谢尔伦2017-05-19 10:20:03
首先在 ES6 中,...
本身就是有结构对象的作用.
const [...a] = [1, 2, 3]
a // [1, 2, 3]
所以对于解构赋值,元素一一对应的关系,题目就分解成了
let [a, b, ...c] = [a, undefined, undefined]
过去多啦不再A梦2017-05-19 10:20:03
这能有啥原因…………
解构赋值和聚合运算就是这么规定的……
赋值运算的右值数组中,第三位及以后都没有元素,那么c
数组当然就是空数组……
迷茫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
数组解构对默认参数和不定参数的规则不同
習慣沉默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没有变
// 循环里面是瞎扯的,但大概是这个原理