首页 > 解决方案 > Javascript:为什么函数返回时数组长度加倍?

问题描述

我正在尝试用 javascript 编写游戏,并且我想要一个大小为 16 的数组,我可以在不同的函数中更新和返回。这是我到目前为止所拥有的,以及为什么它不起作用。

function startgame(){
   var status = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
   console.log(status.length);
   return status;
}

var status = startgame();
console.log("status length now: "+ status.length);

正如预期的那样,函数内的第一个日志显示 16。但是,函数调用后的第二个日志显示“status length now: 31”。返回的数组正在计算所有逗号。我怎样才能避免这种情况?

标签: javascript

解决方案


推荐阅读