Question

I need short messages disappearing after preset time. Please see the fiddle here: http://jsfiddle.net/X88F9/1/.

It works well, what I am not sure about is the reference for each created object:

function addObject() {
    new SomeObj(Math.random() * 1000 + 300);
}

it is not stored in any variable, can I just leave it as it is ? Or do I need to push them in some array ?

I also found this recommendation to put all in closures: https://stackoverflow.com/a/10246262/2969375, but not sure if necessary in my case and if yes, then how.

Was it helpful?

Solution

My answer to the question is: Javascript does not need a reference to the object to work, as proofed by your fiddle. So the question is more about if you need a reference to the object, to do other stuff with it later on. If you, for instance, would like to give the user the ability to click the temporarily display message and stop it from disappearing, than you can put all that code in a closure and do not need a reference, too. But if you would like to display the very same object again after it was removed from the DOM, than you need to store it in an array, other object, or variable, depending on your needs and ways to find it in a list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top