slice 与 splice 的区别

时间:2021-10-17 16:26:36
slice:
  定义一个数组:let b = ['a','b','c','d','e']
  b:["a", "b", "c", "d", "e"]
  b.slice(3,4) :["d"] 截取[3,4)的数组,但是b的值不变
  此时b的值为:
  b:["a", "b", "c", "d", "e"]
splice:
  b :["a", "b", "c", "d", "e"]
  b.splice(0,2) : ["a", "b"] 删除[0,2)的数组,b的值随之改变
  此时b的值为:
  b :["c", "d", "e"]