Destructors?

8 views
Skip to first unread message

Anthony

unread,
Jan 4, 2012, 4:25:06 PM1/4/12
to jsclass-users
Is there a concept of an object destructor in JS.Class? I need to
delete an object and run some logic in the process.

Maël Nison

unread,
Jan 4, 2012, 4:28:14 PM1/4/12
to jsclas...@googlegroups.com
You can't delete an object in Javascript*, and I don't think that it
could be easily simulated.

Can't you just use a classic method then remove every reference to the
instance ?

* Even the delete operator does not delete the object. You must not
use it as the c++ delete operator.

On 4 January 2012 22:25, Anthony <draft...@gmail.com> wrote:
> Is there a concept of an object destructor in JS.Class? I need to
> delete an object and run some logic in the process.

--
Maël Nison
Epitech 2014, Paris - Astek

Anthony

unread,
Jan 4, 2012, 4:47:08 PM1/4/12
to jsclass-users
Hmm I guess I need to read up on the delete keyword.

What I need to do is re-instantiate a JS.Class and replace the
assignment. This is what I'm doing for now:

var myObj = new Foo.Bar.MyObj();
// ... use it for a while... then eventually I need to reconstruct
it...
myObj.destroy() // My own destructor method as you suggested
myObj = new Foo.Bar.MyObj();

On Jan 4, 2:28 pm, Maël Nison <nison.m...@gmail.com> wrote:
> You can't delete an object in Javascript*, and I don't think that it
> could be easily simulated.
>
> Can't you just use a classic method then remove every reference to the
> instance ?
>
> * Even the delete operator does not delete the object. You must not
> use it as the c++ delete operator.
>

Andreas Wehr

unread,
Jan 4, 2012, 5:32:08 PM1/4/12
to jsclass-users
Hi Anthony,
you don't have to care about zombie-objects as the Javascript garbage
collector will remove everything that is not referenced anymore.

foo = new Bar();
foo = null; or foo = new Bar();

should be enough. The old instance will be removed if you don't have
any other references pointing to it.

Anthony

unread,
Jan 4, 2012, 6:42:31 PM1/4/12
to jsclass-users
Thank you =)
Reply all
Reply to author
Forward
0 new messages