HuangXiZhou / blog

自以为历尽沧桑,其实刚蹒跚学步;自以为掌握了竞争的秘密,其实远没有竞争的资格

Home Page:https://blog.trevor.top

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JS小技巧(一)

HuangXiZhou opened this issue · comments

JS删除数组

在JavaScript中,删除一个数组,应该使用arr.length = 0,而不是delete arr

使用arr.length = 0

img-1

使用delete arr

img-2

为什么?

其实在MDN Web Docs中已经给出了答案:

The delete operator removes a property from an object.

Parameters

object:
The name of an object, or an expression evaluating to an >object.

property:
The property to delete.