简体   繁体   English

javascript:删除关联数组的所有对象元素

[英]javascript: remove all object elements of an associative array

I would like to know the most efficient way of emptying an associative array without causing any memory leak (the values are js objects) 我想知道清空关联数组的最有效方法,而不会导致任何内存泄漏(值为js对象)

Just to be sure, I have tried iterating through the elements of the array, calling 'delete' operation for each element object, so that all of the element objects will become candidates for garbage collection. 可以肯定的是,我已经尝试迭代数组的元素,为每个元素对象调用'delete'操作,这样所有的元素对象都将成为垃圾收集的候选对象。 Finally, I reset the assoc array using: somearray = {}; 最后,我使用: somearray = {};重置了assoc数组somearray = {};

Is it necessary to call the 'delete' operation for each of the element? 是否有必要为每个元素调用“删除”操作? Or does resetting the variable value suffice so that all elements will be garbage collected? 或者重置变量值是否足以使所有元素都被垃圾收集?

Thanks! 谢谢!

As long as neither your objects nor the associative array are referenced anywhere else, resetting the array should do it. 只要您的对象和关联数组都没有在其他地方引用,重置数组就应该这样做。

Calling delete on each object is only necessary in IE6 on a DOM element. 只在DOM元素的IE6中调用每个对象上的delete See here 看到这里

somearray.length = 0应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM