let a=[1,2,3,4,5]
let b=a.splice(0)
let c=['a','b','c','d','e']
let d=c.slice(0)
let e=['A','B','C','D','E']
e.splice(0)
let f=['A','B','C','D','E']
f.slice(0)
console.log('a为:', a)
console.log('b为:', b)
console.log('c为:', c)
console.log('d为:', d)
console.log('e为:', e)
console.log('f为:', f)
打印结果:
a为: []
b为: (5) [1, 2, 3, 4, 5]
c为: (5) [“a”, “b”, “c”, “d”, “e”]
d为: (5) [“a”, “b”, “c”, “d”, “e”]
e为: []
f为: (5) [“A”, “B”, “C”, “D”, “E”]
结论:
1、.splice(0) / .slice(0)都可以用于复制数组。
2、.splice(0)会清空原数组,而.slice(0)则不会。
3、.splice(0)不可简写为.splice(),.slice(0)可简写为.slice()。