美文网首页
splice + indexOf 等于隐藏大bug

splice + indexOf 等于隐藏大bug

作者: Mr老朝 | 来源:发表于2022-07-06 12:03 被阅读0次
  • 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]

相关文章

网友评论

      本文标题:splice + indexOf 等于隐藏大bug

      本文链接:https://www.haomeiwen.com/subject/ulcybrtx.html