469,330 Members | 1,235 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 48838
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Stub | last post: by
35 posts views Thread by Peter Oliphant | last post: by
11 posts views Thread by AB | last post: by
23 posts views Thread by Ben Voigt | last post: by
7 posts views Thread by sam | last post: by
6 posts views Thread by Pallav singh | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.