delete
将删除对象属性,但不会为数组重新索引或更新其长度。这使得它看起来好像是未定义的:
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> delete myArray[0]
true
> myArray[0]
undefined
请注意,实际上并没有将其设置为value undefined
,而是从数组中删除了该属性,使其 显得 未定义。Chrome开发人员工具通过empty
在记录阵列时进行打印来明确区分这一点。
> myArray[0]
undefined
> myArray
[empty, "b", "c", "d"]
myArray.splice(start,deleteCount)
实际上会删除元素,为数组重新索引,并更改其长度。
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> myArray.splice(0, 2)
["a", "b"]
> myArray
["c", "d"]