-
Array.prototype.indexOf
如果找不到元素,会返回-1
; -
Array.prototype.splice(-1, 1)
会删掉数组最后一位; - 所以,下面代码将埋下一个bug:
const list = [1, 2, 3];
const a = 4;
list.splice(list.indexOf(a), 1); // 结果list变成[1, 2]
console.log(a); // [1, 2]
Array.prototype.indexOf
如果找不到元素,会返回-1
;Array.prototype.splice(-1, 1)
会删掉数组最后一位;const list = [1, 2, 3];
const a = 4;
list.splice(list.indexOf(a), 1); // 结果list变成[1, 2]
console.log(a); // [1, 2]
本文标题:splice + indexOf 等于隐藏大bug
本文链接:https://www.haomeiwen.com/subject/ulcybrtx.html
网友评论