delete
will delete the object property, but will not reindex the array or update its length.
(delete
将删除对象属性,但不会为数组重新索引或更新其长度。)
This makes it appears as if it is undefined: (这使得它看起来好像是未定义的:)
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> delete myArray[0]
true
> myArray[0]
undefined
Note that it is not in fact set to the value undefined
, rather the property is removed from the array, making it appear undefined.
(请注意,实际上并没有将其设置为undefined
,而是从数组中删除了该属性,使其显得未定义。)
The Chrome dev tools make this distinction clear by printing empty
when logging the array. (Chrome开发人员工具通过在记录阵列时将其打印为empty
使这一区别清晰可见。)
> myArray[0]
undefined
> myArray
[empty, "b", "c", "d"]
myArray.splice(start, deleteCount)
actually removes the element, reindexes the array, and changes its length.
(myArray.splice(start, deleteCount)
实际上会删除该元素,为该数组重新索引并更改其长度。)
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> myArray.splice(0, 2)
["a", "b"]
> myArray
["c", "d"]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…