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

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If you're using jQuery plugins such as DataTables then it does matter: For example, if you intend to re-initialize such a table there is no clean way to do it. See here if you're interested in more details regarding this control and its issues regarding unloading it.
3

here is how:

my_var = null;

//or remove it
delete my_var;

4 Comments

what if the object is referenced only by ' this ' in a function? ' delete this ' doesn't seem to work
I am not sure he has mentioned if it is referenced by this.
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
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)
0

if you define you object as locale like

var obj ={}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.