简体   繁体   English

ES6阵列破坏性怪异

[英]ES6 Array destructuring weirdness

Can anyone explain, why the following happens with ES6 array destructuring? 谁能解释,为什么ES6阵列解构会发生以下情况?

let a, b, c
[a, b] = ['A', 'B']
[b, c] = ['BB', 'C']
console.log(`a=${a} b=${b} c=${c}`)

// expected: a=A b=BB c=C
// actual:   a=BB b=C c=undefined

http://codepen.io/ronkot/pen/WxRqXg?editors=0011 http://codepen.io/ronkot/pen/WxRqXg?editors=0011

As others have said, you're missing semicolons. 正如其他人所说,您缺少分号。 But… 但…

Can anyone explain? 谁能解释?

There are no semicolons automatically inserted between your lines to separate the "two" statements, because it is valid as a single statement. 您的行之间不会自动插入分号来分隔“两个”语句,因为它作为单个语句有效。 It is parsed (and evaluated) as 它被解析(并评估)为

let a = undefined, b = undefind, c = undefined;
[a, b] = (['A', 'B']
[(b, c)] = ['BB', 'C']);
console.log(`a=${a} b=${b} c=${c}`);

wherein 其中

  • [a, b] = …; is a destructuring assignment as expected 是预期的解构任务
  • (… = ['BB', 'C']) is an assignment expression assigning the array to the left hand side, and evaluating to the array (… = ['BB', 'C'])是一个赋值表达式,将数组分配到左侧,并对数组求值
  • ['A', 'B'][…] is a property reference on an array literal ['A', 'B'][…]是数组文字上的属性引用
  • (b, c) is using the comma operator , evaluating to c (which is undefined ) (b, c)使用逗号运算符 ,求值cundefined

If you want to omit semicolons and let them be automatically inserted where ever possible needed, you will need to put one at the begin of every line that starts with ( , [ , / , + , - or ` . 如果你想省略分号,让他们自动插入需要的地方有可能 ,您将需要把一个在开始每一个开头线([/+-`

You've fallen into a trap of line wrapping and automatic semicolon insertion rules in JavaScript. 您陷入了JavaScript中的换行和自动分号插入规则的陷阱。

Take this example: 举个例子:

let x = [1, 2]
[2, 1]

It's the interpreted as: 解释为:

let x = [1, 2][2, 1] // === [1, 2][(2, 1)] === [1, 2][1] === 2

That weird [(2, 1)] thing above is related to how Comma Operator works. 上面的[(2, 1)]很奇怪[(2, 1)]逗号运算符的工作方式有关。

Thus, your example: 因此,您的示例:

let a, b, c
[a, b] = ['A', 'B']
[b, c] = ['BB', 'C']
console.log(`a=${a} b=${b} c=${c}`)

Is interpreted as: 解释为:

let a, b, c
[a, b] = ['A', 'B'][b, c] = ['BB', 'C']
console.log(`a=${a} b=${b} c=${c}`)

Now, if you insert a semicolon, it will work as you intended: 现在,如果插入分号,它将按预期工作:

let a, b, c
[a, b] = ['A', 'B']; // note a semicolon here
[b, c] = ['BB', 'C']
console.log(`a=${a} b=${b} c=${c}`)

Also, it's a good idea to check your code by pasting it into Babel repl to see the generated output: 另外,最好将代码粘贴到Babel repl中以查看生成的输出来检查代码:

'use strict';

var a = void 0,
    b = void 0,
    c = void 0;

var _ref = ['A', 'B'][(b, c)] = ['BB', 'C'];

a = _ref[0];
b = _ref[1];

console.log('a=' + a + ' b=' + b + ' c=' + c);

I believe you have forgotten the line breaks ';'. 我相信您忘记了换行符“;”。 Below is the corrected code. 下面是更正的代码。 Please try: 请试试:

let a,b,c
[a, b] = ['A', 'B'];
[b, c] = ['BB', 'C'];
console.log(`a=${a} b=${b} c=${c}`)
let a, b, c
[a, b] = ['A', 'B']***;***
[b, c] = ['BB', 'C']
console.log(`a=${a} b=${b} c=${c}`)

console: a=A b=BB c=C 控制台:a = A b = BB c = C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM