删除数组中对象的引用 javascript
delete ref to object in array javascript
假设我们有代码:
var players = [];
function Player(){
var num = players.length;
}
players.push(new Player()); //player
我可以从内存中删除这个播放器吗?例如:
players.splice(0, 1);
之后垃圾收集器会收集播放器吗?
我建议看一下 Deleting Objects in JavaScript, When should I use delete vs setting elements to null in JavaScript? and Memory Management(很好的参考,@Karthick)。
Since Javascript is garbage collected, you don't need to delete objects themselves - they will be removed when there is no way to refer to them anymore.
但是,您可以在确定对象已被使用后删除对该对象的引用:
It can be useful to delete references to an object if you are finished with them, because this gives the garbage collector more information about what is able to be reclaimed.
然而,这通常会为您解决,即使您遇到了边缘情况,在小规模应用程序中也不应该是性能问题。
假设我们有代码:
var players = [];
function Player(){
var num = players.length;
}
players.push(new Player()); //player
我可以从内存中删除这个播放器吗?例如:
players.splice(0, 1);
之后垃圾收集器会收集播放器吗?
我建议看一下 Deleting Objects in JavaScript, When should I use delete vs setting elements to null in JavaScript? and Memory Management(很好的参考,@Karthick)。
Since Javascript is garbage collected, you don't need to delete objects themselves - they will be removed when there is no way to refer to them anymore.
但是,您可以在确定对象已被使用后删除对该对象的引用:
It can be useful to delete references to an object if you are finished with them, because this gives the garbage collector more information about what is able to be reclaimed.
然而,这通常会为您解决,即使您遇到了边缘情况,在小规模应用程序中也不应该是性能问题。