ES6解构分配 - 奇怪的表达评估

时间:2015-12-13 11:33:07

标签: javascript ecmascript-6

指向this question我在es6解构赋值中发现了奇怪的行为。

var a = [1, 2, 3];
console.log([] = a); // Array [ 1, 2, 3 ]
console.log([x] = a); // Array [ 1, 2, 3 ]
console.log([x, y] = a); // Array [ 1, 2, 3 ]
console.log(x, y); // 1, 2

我希望看到ouptut像:

Array [ ]
Array [ 1 ]
Array [ 1, 2 ] 

你能解释一下吗?

1 个答案:

答案 0 :(得分:2)

消化可能有点困难,但是this is the official documentation of assignment operators(12.14.4的第一部分特别是rval运算符)。

注意步骤8,其中包含"返回a = b"。这实际上意味着b将返回var a = [1, 2, 3]; console.log([] = a); // [] = a gives a, so print Array [ 1, 2, 3 ] console.log([x] = a); // [x] = a gives a, so print Array [ 1, 2, 3 ] console.log([x, y] = a); // [x, y] = a gives a, so print Array [ 1, 2, 3 ] console.log(x, y); // x and y were destructed correctly, so print 1, 2

所以结果是正确的:

Comparable
相关问题