[英]ES6 destructuring assignment - strange expression evaluation
Pointing to this question I found strange behaviour in es6 destructuring assignment. 指向这个问题,我发现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
I would expect to see ouptut like: 我希望看到ouptut像:
Array [ ]
Array [ 1 ]
Array [ 1, 2 ]
Can you explain this? 你能解释一下吗?
It might be a bit difficult to digest, but this is the official documentation of assignment operators (The first part of 12.14.4 being specifically the =
operator). 可能很难理解,但这是赋值运算符的正式文档 (12.14.4的第一部分专门是
=
运算符)。
Notice step 8 that reads "return rval
". 注意步骤8读取“ return
rval
”。 It essentially means that a = b
will returns b
. 从本质上讲,这意味着
a = b
将返回b
。
So the results are correct: 所以结果是正确的:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.