Do I have to destroy instances myself? …if I don’t assign them a variable…will they automatically go away?
new ImageUploadView();
vs
var Iu = ImageUploadView();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If there is no reference to an object in javascript, the garbage collector will clean it up.
The way the garbage collector works is it looks for javascript objects for which nobody has a reference to. If nobody has a reference to it, then it can’t be used again, so it can be deleted and the memory it occupied reclaimed. On the other hand, if any javascript entity still has a reference to the object, then it is still “in use” and cannot be deleted.
In your first code example:
unless the constructor of the object stores away the
thispointer into some other variable or object or creates some closure that causes references to the object to be held, then there will be no reference to this new object and it will be cleaned up by the garbage collector.If you second code example:
as long as the
Iuvariable exists and stays in scope it will contain whatever theImageUploadView()function returns. Note, the second example, is just executing a function and storing it’s value. It is not necessarily creating anything. IfImageUploadView()just returnstrue, then that’s all theIuvariable will contain.