为什么JS中这两个数组的语法不同?

Edg*_*ero -4 javascript arrays developer-tools

它们都包含相同的值,但我想知道为什么当我在Dev工具中控制它们时它们看起来有所不同.

在此输入图像描述

var values = [6, 6, 6, 19, 13, 50]; 

function boxWidth() {
    var widths = new Array();
    widths.push(values);
    console.log(widths);
    console.log(values);
} 
Run Code Online (Sandbox Code Playgroud)

spa*_*nky 5

widths数组是一个数组,它有一个引用该values数组的成员,而是values一个包含6个数字的数组.

要单独复制数字,请执行以下操作:

widths.push(...values);
Run Code Online (Sandbox Code Playgroud)

或者在创建数组时执行以下操作:

var widths = [...values];
Run Code Online (Sandbox Code Playgroud)

或者是传统兼容:

var widths = values.slice();
Run Code Online (Sandbox Code Playgroud)

您可以序列化阵列以获得更清晰的视图.

var values = [6, 6, 6, 19, 13, 50]; 

function boxWidth() {
    var widths = new Array();
    widths.push(values);
    console.log(JSON.stringify(widths));
    console.log(JSON.stringify(values));
} 

boxWidth();
Run Code Online (Sandbox Code Playgroud)