【发布时间】:2017-12-18 15:49:22
【问题描述】:
我很难理解为什么 console.log() 的位置会影响我的阵列的容量。这是小提琴:https://jsfiddle.net/xbqph81e/4/
代码如下:
const pressed = [];
const secretCode = '123456';
window.addEventListener('keyup', (e) => {
console.log(e.key);
pressed.push(e.key);
console.log("First Log",pressed); //console.log here = array has capacity of 7 characters
pressed.splice(-secretCode.length - 1, pressed.length - secretCode.length);
//console.log("Second Log",pressed); //first console.log is commented and this uncommented = array has capacity of 6 characters
});
.as-console-wrapper { max-height: 100% !important; top: 0; }
有人知道为什么会这样吗?
【问题讨论】:
-
因为您使用
.splice从数组中删除一个?
标签: javascript arrays console.log