IMLC.ME

JavaScript 如何清空一个数组

方法一

arr.length = 0

这个方法看着很 tricky ,但实际上 ES5 里明文规定了其行为,可以放心使用。包括在 strict mode 里。

whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. (15.4Array Objects)[https://262.ecma-international.org/5.1/#sec-15.4]

方法二

arr.splice(0,arr.length)