English 中文(简体)
我如何从物体内明确释放 Java标本?
原标题:How do I explicitly release a JavaScript object from within the object?

http://ej.org/blog/simple-javascript-inheritance/“rel=” John Resig s recipe for Java texts给与遗产。 我 code掉了我的手法,再谈这个问题:

MyClass = Class.extend({

    // create an <h3>Hello world!</h3> in the HTML document
    init : function (divId) { 
        this._divId = divId;
        this._textDiv = document.createElement("h3");
        this._textDiv.innerHTML = "Hello world!";
        document.getElementById(divId).appendChild(this._textDiv);
    },

    // remove the <h3> and delete this object
    remove : function () {
        var container = document.getElementById(this._divId);
        container.parentNode.removeChild(container);

        // can I put some code here to release this object?
    }

});

所有工程都很好:

var widget = new MyClass("theDivId");
...
widget.remove();

我会把其中数百件事放在一页上(显然具有一些敏感的功能),我也想简单地向每个物体释放记忆。 我可以理解,我可以使用<代码>widget = 无;,并在需要时相信GC将物体释放(?),但我在<代码>remove(方法中可以做些什么? 我知道,将<代码>this = 无; at the end of remove( don t work ;]

最佳回答

页: 1 你没有直接接触垃圾收集器的途径。 如你所说,你所能做的最好办法是确保不再提及该物体。

海事组织,它改进了这种方式。 垃圾收集器比你(和我)大得多,因为经过多年的研究,甚至当你尝试和选择时,你仍然可能做不到更好的工作。

当然,如果你重新与一个联合材料发动机相联,那么你将能够控制收缴和收缴部队(更多人)的垃圾,尽管我非常喜欢你担任这一职务。 如果您重新感兴趣,则下载并汇编spi子(或对8,或无论什么发动机标出你的正轨)和在“The >gc”(上。

这使我想到另一点,因为标准并没有界定垃圾收集的内部,即使你能够确定,在你的法典中某个时候援引烟 g是有帮助的,这很可能不会在所有平台上获得同样的惠益。

问题回答

there is no ways to destroy objects manually, only way is to free all links to your object and trust removal to GC actually in your code you should clear this._textDiv = null and container = null in remove method too, because it can be a problem for GC in some browsers

<代码>该是一个关键词,你不能给这些词分配任何价值。 从范围上去除物体的唯一途径是将<代码>null<>/code>人工分配给每个变量。

然而,这种方法并不总是奏效的: 在《XMLHttpRequest》的某些实施中,必须在XMLHttpRequest从记忆中解放出来之前重新制定onreadystate<>/code>和 open功能。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.