javascript - es6 array destructuring assignment
伊谢尔伦
伊谢尔伦 2017-05-19 10:18:03
0
4
482

Please explain the reason, especially c

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(4)
伊谢尔伦

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梦

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 carray is of course an empty array...

迷茫
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

習慣沉默
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没有变
// 循环里面是瞎扯的,但大概是这个原理



Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template