Javascript 如果存在';没有阵列?
我被这项任务困住了。如何使用数组分解将下面的三条语句更改为一条语句 我感到困惑的主要原因是a和b已经声明了。而且没有数组 'Javascript 如果存在';没有阵列?,javascript,arrays,Javascript,Arrays,我被这项任务困住了。如何使用数组分解将下面的三条语句更改为一条语句 我感到困惑的主要原因是a和b已经声明了。而且没有数组 ' (function UseArrayDestructuring2() { let a = 1; let b = 2; // Use array destructuring to change the 3 statements below into 1 statement. // You sh
(function UseArrayDestructuring2() {
let a = 1;
let b = 2;
// Use array destructuring to change the 3 statements below into 1 statement.
// You should not need a temporary variable anymore.
let tmp = a;
a = b;
b = tmp;
// Don't make changes below this line
expect(a).toEqual(2);
expect(b).toEqual(1);
''''您可以收集数组中的值,并将数组分解为切换的变量
(function UseArrayDestructuring2() {
let a = 1;
let b = 2;
// Use array destructuring to change the 3 statements below into 1 statement.
// You should not need a temporary variable anymore.
let tmp = a;
a = b;
b = tmp;
// Don't make changes below this line
expect(a).toEqual(2);
expect(b).toEqual(1);
这种方法仍然会创建一个临时值
[b, a] = [a, b];
你可以创建一个数组,然后将其解构。这看起来像是一个代码评估…这是否回答了你的问题?(特别是。)它创建一个临时值,即数组,但不创建其他变量。