473,320 Members | 1,990 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,320 software developers and data experts.

How to implement a destructor?

Hi,

I'm looking for a way to implement a destructor as a member method of a
class.

/* BEGIN CODE */

function SomeClass() {

this.constructor.numInstances++;

this.destroy = function () {

this.constructor.numInstances--;
/*
Need someway here to delete the instance
*/

};
}

SomeClass.numInstances = 0;

sc = new SomeClass();

// SomeClass.numInstances = 1

sc.destroy();

// SomeClass.numInstances = 0 and sc is not referenced anymore

/* END CODE */

If there was only a delete operation involved (as in -- delete sc -- in
the above exemple) , I wouldn't need a destructor, but since there is
one operation more (decrementing the number of instances; and there
could be more), and I do not want to delegate the full destruction
process to the procedure which instantiated the object, I need to
implement a destructor as a member method.

Any help would be appreciated on this.

F.

Apr 8 '06 #1
11 52689
François wrote:
Hi,

I'm looking for a way to implement a destructor as a member method of a
class.

Why? delete in JavaScript isn't the same as in Java or C++.

--
Ian Collins.
Apr 9 '06 #2
Ian,

I need to implement it so I can delete an instance and provide at the
same time a chain of actions triggered by the destruction:

sc.destroy() --> deletes sc and --> decrements the number of instances
and --> do some other action (triggers an event, pops up an alert("The
object sc has been destroyed") or some other actions, all part of the
destruction process)

I could do all this outside the class, in the calling procedure:

sc = new SomeClass();
delete sc;
SomeClass.constructor.numInstances--;
event = new CustomEvent();
alert("The object sc has been destroyed");

It seems to me that it would break encapsulation: the SomeClass class
bleeds outsides its boundaries.

Thanks for the reply

F.

Apr 9 '06 #3
Ian,

I need to implement it so I can delete an instance and provide at the
same time a chain of actions triggered by the destruction:

sc.destroy() --> deletes sc and --> decrements the number of instances
and --> do some other action (triggers an event, pops up an alert("The
object sc has been destroyed") or some other actions, all part of the
destruction process)

I could do all this outside the class, in the calling procedure:

sc = new SomeClass();
delete sc;
SomeClass.constructor.numInstances--;
event = new CustomEvent();
alert("The object sc has been destroyed");

It seems to me that it would break encapsulation: the SomeClass class
bleeds outsides its boundaries.

Thanks for the reply

F.

Apr 9 '06 #4
François wrote:
Ian,
You will have more success on Usenet if you learn and follow the normal
Usenet posting conventions. See:-

<URL: http://www.jibbering.com/faq/ >
I need to implement it so I can delete an instance and provide
at the same time a chain of actions triggered by the destruction:

sc.destroy() --> deletes sc

<snip>

Calling a method of an object that cleans up (in some sense) can work
effectively on the internal details of the object but you are not going
to have much joy trying to act upon what in practice is an arbitrary
Identifier referring to the object in some arbitrary scope.

You don't really need to delete the Identifier (and if you declared it
you probably cannot delete it anyway as declared variables are marked
'DontDelete' when they are created). It would be sufficient in practice
to assign the Undefined or Null values to those Identifiers (which
should free the objects referred to for garbage collection).

I suspect that insisting that your destructor is called as:-

sc = sc.destroy();

- and having the method return null explicitly or return nothing
(effectively the Undefined value) is the best you can achieve as far as
acting upon the Identifier is concerned.

Richard.
Apr 9 '06 #5
Thank you: simple and neat solution, that does exactly what I need.

About posting conventions: where did I stray from them? I do indeed
want to respect the list rules.

Regards,

F.

Apr 9 '06 #6
François wrote:
Thank you: simple and neat solution, that does exactly what I need.

About posting conventions: where did I stray from them? I do indeed
want to respect the list rules.

Have a look at <http://cfaj.freeshell.org/google/>.

--
Ian Collins.
Apr 9 '06 #7
Got it: thanks.

F.

Ian Collins wrote:
Have a look at <http://cfaj.freeshell.org/google/>.

--
Ian Collins.


Apr 9 '06 #8
François wrote:
<snip>
About posting conventions: where did I stray from them?
I do indeed want to respect the list rules.


You failed to appropriately quote the message that you are responding
to. See the FAQ:-

<URL: http://www.jibbering.com/faq/ >

- and the more detailed explanation of posting style that it links to.
Then, if the explanation provided is not sufficiently clear, ask for
clarification. (And don't let yourself be fooled into thinking that the
way in which you are viewing this newsgroup has any baring on how anyone
else may be viewing it.)

Richard.
Apr 9 '06 #9
François wrote:
Got it: thanks. <snip>

I am afraid that you have not yet 'got it'. You have gone from not
quoting the message you are responding to, to top-posting and failing to
appropriately trim the material that you are quoting.
Ian Collins wrote:

<snip>

Richard.
Apr 9 '06 #10
JRS: In article <11**********************@i40g2000cwc.googlegroups .com>
, dated Sat, 8 Apr 2006 21:38:25 remote, seen in
news:comp.lang.javascript, =?iso-8859-1?B?RnJhbudvaXM=?=
<fr*************@gmail.com> posted :
Thank you: simple and neat solution, that does exactly what I need.

About posting conventions: where did I stray from them? I do indeed
want to respect the list rules.


If you had been told what you did wrong, that would, hopefully,
eliminate one class of error from your future posts.

You were told where to read about conventions, which *should* eliminate
*all* of the well-known errors. See also via sig line 2 below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Apr 9 '06 #11

"Dr John Stockton" <jr*@merlyn.demon.co.uk> kirjoitti viestissä
news:2s**************@merlyn.demon.co.uk...

....
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME © Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A. Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

"about usage of News.No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail

News"

Why not to add:

"Be as harsh, unfriendly, aggressive as those wise oldtimers, who must(?)
answer dumb questions constantly.'
Apr 10 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
3
by: Brett Hall | last post by:
I have a VB.NET interface that my managed C++ code is to implement. I seem to be stuck implementing an event defined in that interface. Does anyone have a simple code snippet that will show me...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
11
by: AB | last post by:
Hi All, I've got an array of objects, during the execution of the program I'd like to assign a particular object to a certain element in the object array. The sample code's like this... class...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
7
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
4
by: Zytan | last post by:
I know two possible ways: 1. Use AppDomain.DomainUnload Event http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemAppDomainClassDomainUnloadTopic.asp which is kinda like Turbo Pascal,...
2
by: ecestd | last post by:
how do you implement a copy constructor for this pointer-based ADT queue #include "Queuep.h" #include <cassert> #include <new> using namespace std; Queue::Queue () : backPtr (0), frontPtr(0)...
6
by: Pallav singh | last post by:
Hi All How Does compiler implement virtual destructor ??? as we all know if base class destructor is virtual..........then while wriiting statemnt like this Base * b = new Derived( );...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.