HighDots Forums  

constructor/destructor

Javascript JavaScript language (comp.lang.javascript)


Discuss constructor/destructor in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Une Bévue
 
Posts: n/a

Default constructor/destructor - 06-01-2008 , 04:01 PM







in a script (a simplified L-System) i do have objects constructing other
(children) objects by :
this.createChild=function(){
return new Ant(...);
}

all of the children finished their job before the parent one then, i
wonder how to destroy (nullify?) those no more usefull objects.

saying 'o' is one of this new object i want to "destroy" it, is it
siffuciant to do :

o=null;

???

even, is it usefull (freeing memory?)

notice, by construction all of the children of a given parent, finished
them job before the parent (the destruction could only be done from the
parent, from where the child is named-identified).
--
Une Bévue

Reply With Quote
  #2  
Old   
Joost Diepenmaat
 
Posts: n/a

Default Re: constructor/destructor - 06-01-2008 , 04:55 PM






unbewusst.sein (AT) weltanschauung (DOT) com.invalid (Une Bévue) writes:

Quote:
in a script (a simplified L-System) i do have objects constructing other
(children) objects by :
this.createChild=function(){
return new Ant(...);
}

all of the children finished their job before the parent one then, i
wonder how to destroy (nullify?) those no more usefull objects.

saying 'o' is one of this new object i want to "destroy" it, is it
siffuciant to do :

o=null;

???
Depends on how you define "sufficient". The emacscript spec does not
define any garbabage collection mechanism, but its pretty obvious that
any sane and efficient mechanism will only collect unreachable
objects. IOW, you need to "clear out" any references to objects you
want to clean up, either by explicitely assigning them some other
value (as you do here), or by letting the references go out out scope.

Quote:
even, is it usefull (freeing memory?)
Depends on the implementation. In most browsers, it is, more or
less. But you probably shouldn't rely on the memory being freed
quickly unless you exit the page you're on and/or are running out of
memory.

Quote:
notice, by construction all of the children of a given parent, finished
them job before the parent (the destruction could only be done from the
parent, from where the child is named-identified).
I don't understand what you mean. note that as I stated above there is
no portable way to enforce destruction at all. you can only make
object *eligible* for destruction/collection; you can't destruct or
collect them "manually". the key to making object eligible for
deletion is to make them unreachable.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


Reply With Quote
  #3  
Old   
Une Bévue
 
Posts: n/a

Default Re: constructor/destructor - 06-02-2008 , 01:08 AM



Joost Diepenmaat <joost (AT) zeekat (DOT) nl> wrote:

Quote:
the key to making object eligible for
deletion is to make them unreachable.
OK, thanks, that's clear enough to me.
--
Une Bévue


Reply With Quote
  #4  
Old   
Une Bévue
 
Posts: n/a

Default Re: constructor/destructor - 06-02-2008 , 01:56 AM



Joost Diepenmaat <joost (AT) zeekat (DOT) nl> wrote:

Quote:
IOW, you need to "clear out" any references to objects you
want to clean up, either by explicitely assigning them some other
value (as you do here), or by letting the references go out out scope.
in fact, after your answer, i've tried tu nullify the reference to the
object instance, without any effect, by doing simply :

a_child=null;// from parent

my object is a tree, where i create branches, when a branch (another
instance of a tree) is finished to display (the objects are drawing
lines over a canvas), i want to destroy it.

the script reads a rule (a string) like that :
"F+[-F...]+F..."

when a parent reads a "[" symbol it creates a child which reads the
(sub)rule to it's closing "]", it is there i want to destroy the child.

for the time being the only way i've found to stop the child is to
advance it's index to the end of the rule.

line 126 :
case ']':
this.parent.restore(this);// returns action to the parent
this.index=this.rule.length-1;// go to the end of rule
break;

line 71 :
this.restore=function(child){
this.penUp();
ctx.moveTo(this.x,this.y);
this.penDown();
this.index=child.index+1;// continue action where the child left it
this.actRule(this.rule);
return this;
};

my testing code is at the page
<http://thoraval.yvon.free.fr/Canvas/l_system.xhtml>


--
Une Bévue


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.