垃圾回收和JavaScript“删除":这是过分的/混淆性还是好的做法? [英] Garbage Collection and JavaScript "delete": Is this overkill/obfuscation, or a good practice?
问题描述
我刚刚阅读了这个问题和可接受的答案:什么是JavaScript垃圾回收?
I just read this question and the accepted answer: What is JavaScript garbage collection?
在回答中, Noldorin 引用了Apple的一些准则.这是我关心的部分:
In the answer, Noldorin referenced some guidelines from Apple. Here is the part I'm concerned with:
使用删除语句.每当使用新语句创建对象时,将其与delete语句配对.这样可以确保与对象关联的所有内存(包括其属性名称)都可用于垃圾回收.
Use delete statements. Whenever you create an object using a new statement, pair it with a delete statement. This ensures that all of the memory associated with the object, including its property name, is available for garbage collection.
我总是花时间跟上最佳实践的步伐,尤其是在我可以减少脚本的内存占用的情况下.所以我去测试一些东西.如果我理解正确,下面是一个对象的示例,该对象在调用方法后会自行删除.
I'm always taking time to keep up-to-speed on best practices, especially if I can reduce the memory footprint of my scripts. So I went off to test some things. If I understand correctly, the following is an example of an object that deletes itself after invoking a method.
var thing = function () {
var a_method, and_another;
a_method = function() { /* do stuff */ };
and_another = function() { /* do some other stuff*/ };
this.init = function() { a_method(); and_another(); };
};
delete new thing().init();
通常,我将所有内容包装在一个自调用函数中,并像上面那样传递我的全局变量.一切都和我通常做的一样,唯一的不同是我在new
之前添加了delete
.
Usually I'll wrap everything in a self invoking function and pass in my globals just like above. Everything is the same as I would normally do it, the only difference being that I added the delete
right before the new
.
代码以任何一种方式起作用.
The code works either way.
所以问题是:我在这里做什么 吗?删除对仅存在于功能范围内的对象的引用是否有某种好处?还是我只是让事情看起来混乱?
So the question is: Am I doing anything here? Is there some kind of benefit to deleting a reference to an object that only exists inside a function scope? Or am I just making things look confusing?
推荐答案
首先,语句delete new scoped_object().init();
并没有真正做任何事情,您最好注意哪些变量仍处于封闭状态,或者如果您有循环引用,这是内存泄漏的最常见来源.
First of all the statement delete new scoped_object().init();
is not really doing anything, you should better take care about what variables remain in-closure or if you have circular references, which are the most common source of memory leaks.
delete
运算符用于删除对象属性,并且确实被误解了,您从@Noldorin引用的答案引用了一些 Apple JavaScript最佳做法" ,但是他们对delete
的工作原理一无所知.
The delete
operator is meant to be used to delete object properties, and it is really misunderstood, the answer you quote from @Noldorin quotes some text of the Apple JavaScript "Best Practices", but they don't have a clue about how delete
works!!.
他们甚至建议在变量引用上使用delete
,这是不可能的-仅适用于在 Eval Code 中声明的变量,因为var
语句将变量声明为不可删除(在ECMAScript 3中为{DontDelete}
,在ECMAScript 5中为[[Configurable]] = false
)的可变对象-构成作用域链的对象的属性.
They even recommend using delete
on variable references, and that is not possible -only possible for variables declared in Eval Code-, because the var
statement declares the variable as non-deletable ({DontDelete}
in ECMAScript 3, or [[Configurable]] = false
in ECMAScript 5) properties of the Variable Object -objects that form the scope chain-.
此外,尝试delete
引用绑定到环境记录的标识符-用VariableDeclaration
,FunctionDeclaration
或从函数的FormalParameterList
-声明的标识符,会导致SyntaxError
异常在新的 ECMAScript 5th Edition 下的
Moreover, attempting to delete
a reference to an identifier that is bound to an environment record - an identifier declared with a VariableDeclaration
, FunctionDeclaration
or from a function's FormalParameterList
-, causes a SyntaxError
exception on the new ECMAScript 5th Edition under Strict Mode.
我建议您阅读以下有关delete
的文章:
I would recommend you to read the following article about delete
:
这篇关于垃圾回收和JavaScript“删除":这是过分的/混淆性还是好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!