Javascript 中 Slice 和 Splice 方法之间的低级差异

javascriptfront end technologyobject oriented programming更新于 2025/11/21 8:44:17

slice 和 splice 之间的基本差异是 −

  • splice() 会更改调用它的原始数组,并将数组中已删除的项目作为新数组对象返回。

  • slice() 不会更改原始数组,还会返回切片后的数组。

示例

// splice 会更改数组
let arr = [1, 2, 3, 4, 5];
console.log(array.splice(2));
//slice 不会更改原始数组
let arr2 = [1, 2, 3, 4, 5];
console.log(array2.slice(2));
console.log("
更改数组后"); console.log(array); console.log(array2);

输出

[ 3, 4, 5 ]
[ 3, 4, 5 ]

更改数组后

[[ 1, 2 ]
[ 1, 2, 3, 4, 5 ]

相关文章


有用资源