Javascript allows swapping of variables:
var x = 1
var y = 2
[x, y] = [y, x] // y = 1 , x = 2
And destructured assignment:
var a, b
[a, b] = [1, 2]
log(a) // 1
log(b) // 2
When using variable swapping in lieu with destructured assignment, trying to swap variables breaks down:
var a, b
[a, b] = [1, 2] // a = 1, b = 2
[a, b] = [b, a] // TypeError: Cannot set property '2' of undefined
Why is that?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…