最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ES6 Array destructuring weirdness - Stack Overflow

programmeradmin2浏览0评论

Can anyone explain, why the following happens with ES6 array destructuring?

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

Can anyone explain, why the following happens with ES6 array destructuring?

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

Share Improve this question edited Jun 7, 2024 at 14:44 VLAZ 29k9 gold badges62 silver badges83 bronze badges asked Jun 27, 2016 at 9:23 ronkotronkot 6,3474 gold badges30 silver badges43 bronze badges 5
  • 5 put ; after each line – Pranav C Balan Commented Jun 27, 2016 at 9:26
  • yup, tested that, @PranavCBalan's comment works – Tschallacka Commented Jun 27, 2016 at 9:28
  • 3 This is why ASI is a misfeature. – Ry- Commented Jun 27, 2016 at 9:31
  • True, @PranavCBalan's solution works. Wow, for me this is first case where the semicolons really matter in js code. I prefer semicolon-less code for cleaner syntax, but maybe I have to start to use them. Can anyone explain, why in this case the semicolons are really needed? – ronkot Commented Jun 27, 2016 at 9:36
  • 2 @ronkot Almost any time you start a line with array indexer or parenthesis you're going to have an issue (Example). Most devs who prefer semi-colonless start the line with a ; to fix ASI related issues. – CodingIntrigue Commented Jun 27, 2016 at 10:52
Add a comment  | 

4 Answers 4

Reset to default 16

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 = undefined, 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
  • ['A', 'B'][…] is a property reference on an array literal
  • (b, c) is using the comma operator, evaluating to c (which is undefined)

If you want to omit semicolons and let them be automatically inserted where ever possible needed, you will need to put one at the start of every line that begins with (, [, /, +, - or `.

You've fallen into a trap of line wrapping and automatic semicolon insertion rules in 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.

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:

'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

发布评论

评论列表(0)

  1. 暂无评论