javascript - 使用 Array.fill() 在没有引用的情况下在一行中声明 ES6 多个变量会导致问题
问题描述
据我所知,如果我们使用 创建多个变量Array().fill()
,它将创建唯一变量而没有任何引用。下面是两个代码块和输出。
let [a, b] = Array(2).fill(false);
console.log(a, b); // Output: false false
a = true;
console.log(a, b); // Output: true false
let [x, y] = Array(2).fill([]);
console.log(x, y); // Output:[] []
x.push(1);
console.log(x, y); // Output:[1] [1]
第一个代码块没问题,没有参考。但是第二个会发生什么。我期待输出“[1] []”。请澄清这里发生了什么。
解决方案
推荐阅读
- sas - SAS:以特定方式显示数据
- haskell - 分解类型作为折叠中的种子
- jsdoc - jsdoc - 为多个功能重用文档?
- android - 如何知道 AsyncTask 是否在后台工作?
- angular - Angular 5 输入变化
- android - Google Assistant,将已安装的应用程序与没有 OAuth2 的助手连接
- ios - 逐字符动画 CATextLayer
- c# - C#读取多部分流
- angular5 - 使用 yarn webpack:prod 时无法解析 app.module.ngfactory
- python - 在 Python 中尝试 / except 以显示哪个文件引发了错误