tags:

views:

62

answers:

3

Please excuse my question if it doesnt make much sense.

I am using javascript to create a dom element to do that i create an object ( obj ={}) and fill out the properties as i go, one of which is the dom element to be created. once the element is created and appended to the document i do not need the object to occupy any space in the memory so i was thinking i should remove it. how would i go about doing that? thank you

+1  A: 

here is how:

my_var = null;

//or remove it
delete my_var;
Sarfraz
what if the object is referenced only by ' this ' in a function?' delete this ' doesn't seem to work
salmane
I am not sure he has mentioned if it is referenced by this.
Sarfraz
I don't think you can delete an object from within one of its member functions. Keep a reference to it and delete it somewhere else
tm1rbrt
there's a common misconception about what `delete` does: it's not like C++' `delete`, ie it won't trigger object destruction; all it does is remove properties from objects - it doesn't work on local variables at all; `delete foo.bar` and `delete foo[42]` are ok, whereas `delete foo` will do nothing if `foo` is a local variable (and even throw an syntax error in strict-mode ECMAScript 5)
Christoph
+3  A: 

The object's going to exist in memory as soon as it's in the DOM, and the obj property that holds it is really holding a reference to it, not a copy. So as far as I know, the memory required to keep the reference as a property of obj should be negligible. In which case I wouldn't worry about removing it at all.

Karl B
A: 

if you define you object as locale like

var obj ={}

it will be automatically undefined at the end of the function.