469,270 Members | 1,664 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

JavaScript object destructor method ???

Eli
Hi,

Is there any way that I can define a destructor for an object in
JavaScript that will be called automatically when object is discarded?
<script type="text/javascript">
MyOBJ.prototype=new Object();
MyOBJ.prototype.constructor=MyOBJ;
function MyOBJ()
{
alert('Constructor!');
}
MyOBJ.prototype.destructor=function()
{
alert('Destructor!');
}

var my_obj=new MyOBJ(); //<-- will show 'Constructor!'
delete my_obj; //<-- will show 'Destructor!'
</script>
-thanks in advance, Eli

Jul 23 '05 #1
5 22086
VK
Memory alloc/calloc in JavaScript works similar to Java. You don't need
and you really cannot free the memory directly, because you don't have
a direct memory access. You can remove a reference to a custom object
by doing myObject = null;
If it was the last reference to the instance, it gets marked as free to
remove and it will be eventually removed by the garbage collector.

Jul 23 '05 #2
Eli
But what if I want to delete all the references to an object?
I got some references to the same object, and I want to delete the
object totally in all references. Is it possible?

-thanks, Eli

Jul 23 '05 #3
VK
Sure! Just keep track of all references you are creating ;-) and don't
make more references than it's necessary ;-)
If the task could be solved globally by some simple trick, we would not
hear about memory leaks (but we do).

Jul 23 '05 #4
"Eli" <el*****@gmail.com> writes:
But what if I want to delete all the references to an object?
Feel free. That's not guaranteed to delete the object, though.

ECMAScript makes no requirements on memory management, so there is no
guarantee that any object is ever destroyed. Some implementations will
indubiably use a grabage collector, while others will merely keep
allocating until the page is unloaded.
I got some references to the same object, and I want to delete the
object totally in all references. Is it possible?


Given only one reference, no.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #5
Eli wrote:
Is there any way that I can define a destructor for an object in
JavaScript that will be called automatically when object is discarded?


No. Why do you think you need to?
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Philip | last post: by
2 posts views Thread by One Handed Man [ OHM# ] | last post: by
5 posts views Thread by Digital | last post: by
2 posts views Thread by zzorozz | last post: by
1 post views Thread by KDawg44 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.