What I am trying to work out is whether nullifying is always necessary. As I am having a few issues with Chrome. Say I have this code.
function MyClass() {}
MyClass.prototype.MyFunction = function() {
this.data = "foo";
}
var c = new MyClass();
c.MyFunction();
Now once that function is called it should be allowed to be GC but should the end of the function have this.data = null. Should this also be standard.
1 Answer 1
Your code will not work. You should first create an instance of your class:
var c = new MyClass();
c.MyFunction();
because MyFunction is instance function.
Otherwise there's also delete operator (reference and an indepth analysis), that is used to remove object members (but not objects themselves). Objects can therefore be garbage collected when there's no way to reference them any more, so
c = undefined;
should convince Javascript to garbage collect this object instance and release memory resources taken by it.
It is different if your object instance is instantiated this way:
c = new MyClass();
c.MyFunction();
delete c; // success
because c is this time a member of global (window) and can therefore be deleted from it.
5 Comments
Explore related questions
See similar questions with these tags.
MyClass?