473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 22542
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Midas | last post by:
Greetings everyone, I'm including code, for cut/paste, to better explain my question. I create a Car object then I call some of its methods. No problem. Then I try to pass, to a function, the name...
7
by: Philip | last post by:
Hi all, Been having a really tricky problem. I'm trying to do some object oriented programming with javascript and it was all working fine until I had to support Netscape 4. The problem is...
2
by: One Handed Man [ OHM# ] | last post by:
The help for the .NET Framework Class Library tells us that the Object.GetHashCode() Method does not guarantee uniqueness' or consistency and that overriding this and the Equals method is a good...
5
by: Digital | last post by:
Hi, I'm looking for links to the JS object model for the not-as-popular properties and methods. I've found things like hasOwnProperty, isEnumerable, ===, prototype, and others by searching for...
2
by: mikevanoo | last post by:
Hi, Can anyone work out why the following doesn't work. For some reason, when you dynamically add a button to the document from a Javascript object it can't then reference the properties of the...
2
by: zzorozz | last post by:
I often find that I need to pass a non-static method as a call-back instead of a functor or a function. The problem with that is the implicit 'this' parameter. So I created an adaptor that given...
1
by: KDawg44 | last post by:
HI, I am using jsgraph to draw some graphs. I want to be able to update these graphs by calling a javascript function to update data. I have a div to display the graph that looks like this: ...
23
Frinavale
by: Frinavale | last post by:
JavaScript is a very strange place for me... So, I decided that, before I attempt to create an Object, I should first learn about Objects. I had actually created one before with the help of a...
3
Frinavale
by: Frinavale | last post by:
I've created a few ASP.NET Ajax Enable Server controls. There are 2 components to these controls: a server side Object that deals with the server side stuffs, and a client side Object that deals...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.