简体   繁体   中英

How do I explicitly release a JavaScript object from within the object?

I'm using John Resig's recipe for JavaScript 'classes' and inheritance. I've stripped my code back to something like this for this question:

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?
    }

});

All works well:

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

I'm going to have hundreds of these things on a page (obviously with some sensible functionality) and I'd like a simple way to release the memory for each object. I understand I can use widget = null; and trust the GC releases the object when required (?), but can I do something explicit in the remove() method? I know that placing this = null; at the end of remove() doesn't work ;)

没有办法手动销毁对象,唯一的方法是释放所有链接到你的对象并信任删除到GC实际上在你的代码中你应该清除this._textDiv = nullcontainer = null in remove方法,因为它可以是一个某些浏览器中的GC问题

No. You don't have any way of accessing the garbage collector directly. As you say, the best you can do is make sure the object is no longer referenced.

IMO, it's better that way. The garbage collector is much smarter than you (and me) because years of research has gone into writing the thing, and even when you try and make optimisations, you're likely still not doing a better job than it would.

Of course if you're interfacing with a JS engine you will be able to control the execution and force garbage collection (among much more), although I very much doubt you're in that position. If you're interested, download and compile spider monkey (or v8, or whatever engine tickles your fancy), and in the repl I think its gc() for both.

That brings me to another point, since the standard doesn't define the internals of garbage collection, even if you manage to determine that invoking the gc at some point in your code is helpful, it's likely that that will not reap the same benefits across all platforms.

this is a keyword, to which you cannot assign any value. The only way to remove objects from a scope is to manually assign null to every variable.

This method doesn't always work, however: In some implementations of the XMLHttpRequest, one has to reset the onreadystate and open functions, before the XMLHttpRequest object is freed from the memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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