How to quickly clear a JavaScript Object?

后端 未结 8 1143
一个人的身影
一个人的身影 2020-11-27 12:52

With a JavaScript Array, I can reset it to an empty state with a single assignment:

array.length = 0;

This makes the Array \"appear\" empty

8条回答
  •  醉话见心
    2020-11-27 13:21

    You can delete the props, but don't delete variables. delete abc; is invalid in ES5 (and throws with use strict).

    You can assign it to null to set it for deletion to the GC (it won't if you have other references to properties)

    Setting length property on an object does not change anything. (it only, well, sets the property)

提交回复
热议问题