在 Javascript 中的同一变量上没有 `delete` 的 `new`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4869712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
`new` without `delete` on same variable in Javascript
提问by Mateen Ulhaq
Is it OK to do this?:
这样做可以吗?:
function mygetTime()
{
var d = new Date();
return(d.getTime());
}
function wasteSomeMemory()
{
var temp;
for(var count = 0; count < 1000000; count += 1)
{
temp = mygetTime();
}
}
Will calling wasteSomeMemory()cause a memory leak?
调用wasteSomeMemory()会导致内存泄漏吗?
What about this:
那这个呢:
function wasteSomeMemory2()
{
var temp;
for(var count = 0; count < 1000000; count += 1)
{
temp = new Date();
}
}
Will calling wasteSomeMemory2()cause a memory leak? Should I use delete temp;at the end of the for-loop?
调用wasteSomeMemory2()会导致内存泄漏吗?我应该delete temp;在 for 循环结束时使用吗?
function wasteSomeMemory2()
{
var temp;
for(var count = 0; count < 1000000; count += 1)
{
temp = new Date();
delete temp;
}
}
回答by T.J. Crowder
newand deletehave nothing whatsoever to do with each other in JavaScript (despite their confusing similarity to completely different constructs in other languages). Don't worry about creating objects (new) without explicitly cleaning them up, that's the garbage collector's job.
new并且delete在 JavaScript 中彼此之间没有任何关系(尽管它们与其他语言中完全不同的构造具有令人困惑的相似性)。不要担心创建对象 ( new) 而不明确清理它们,这是垃圾收集器的工作。
newis for creating objects via constructor functions. delete, on the other hand, is for removing properties from objects. It has nothingto do with removing an object from memory, other than as a side effect (e.g., if the only outstanding reference to that object was from the property that you removed).
new用于通过构造函数创建对象。delete,另一方面,用于从对象中删除属性。它与从内存中删除对象无关,除了作为副作用(例如,如果对该对象的唯一未完成引用来自您删除的属性)。
Example of correct use of delete:
正确使用的示例delete:
var obj = {};
obj.foo = "bar"; // Now `obj` has a property called `foo`
delete obj.foo; // Now it doesn't
Your getmyTimefunction is perfectly fine. The Dateobject will become eligible to be reclaimed immediately upon function return (whether it isreclaimed is completely down to the implementation). It does not cause a memory leak, except on a buggy implementation.
你的getmyTime功能非常好。该Date对象将成为资格函数返回时立即收回(无论是回收是完全下降到实现)。它不会导致内存泄漏,除非有错误的实现。
Your wasteSomeMemory2similarly doesn't cause a memory leak, and in fact you can'tcall delete temp; — you can only delete properties, not vars.
你wasteSomeMemory2同样不会导致内存泄漏,事实上你不能调用delete temp; ——你只能删除属性,不能删除变量。
There aretimes when you have to help the garbage collector out, but those usually don't (in my experience) have to do with object properties and so don't involve delete. They only really come up when you're creating function instances (which is fairly often, if you're setting up event handlers or timer functions, etc.). For instance, consider:
这里是当你有帮助垃圾收集器了几次,但那些通常不会(在我的经验)有对象属性做,因此不涉及delete。它们只有在您创建函数实例时才会真正出现(如果您正在设置事件处理程序或计时器函数等,这种情况很常见)。例如,考虑:
function foo() {
var listOfThings = /* ...get a list of things... */;
// ...do something with `listOfThings`...
setInterval(function() {
// ...do something that *doesn't* need `listOfThings`...
}, 1000);
}
Because your anonymous function you've assigned to a timer via setIntervalwill survive the function call, it keeps a live reference to everything that was in-scope during that function call (whether it uses it or not). This keeps the list of things that listOfThingspoints to in memory. If the timer function doesn't need that list, that's a concern. You can release the list that listOfThingspoints to if you know that the function doesn't need it, by assigning undefinedor nullor whatever to listOfThingswhen you're done with it:
因为您分配给计时器的匿名函数setInterval将在函数调用中继续存在,所以它会保留对该函数调用期间范围内所有内容的实时引用(无论是否使用它)。这会保留listOfThings指向内存的事物列表。如果计时器功能不需要该列表,那就是一个问题。您可以释放列表listOfThings点,如果你知道该函数并不需要它,通过分配undefined或null或任何对listOfThings当你用它做:
function foo() {
var listOfThings = /* ...get a list of things... */;
// ...do something with `listOfThings`...
listOfThings = undefined; // Done with it <== The new bit
setInterval(function() {
// ...do something that *doesn't* need `listOfThings`...
}, 1000);
}
The same is true for event handler functions, etc. Whenever you create a function, it "closes over" (keeps a live reference to) anything in scope where it was defined. So if you don't need those things, you can ensure they're not kept in memory by clearing the references to them. (More: Closures are not complicated)
事件处理函数等也是如此。每当您创建一个函数时,它都会“关闭”(保持对它的实时引用)定义它的范围内的任何内容。因此,如果您不需要这些东西,您可以通过清除对它们的引用来确保它们不会保留在内存中。(更多:闭包并不复杂)
回答by Raynos
The short answer is no.
最简洁的答案是不。
The long answer is we hope to god the browses garbage collector picks this up.
长长的答案是我们希望浏览垃圾收集器能把它捡起来。

