Re: Local / lazy referencing

Brian Alliet <[email protected]> Thu, 2 Oct 2003 09:31:51 -0400
Newsgroups gmane.comp.java.xwt.widgets
Message-ID <[email protected]>
On Thursday, October 2, 2003, at 07:10  AM, Charles Goodwin wrote:

> Is this going to work:
>> Press1 += function() {
>>     if ( enabled ) {
>>         activated = true;
>>         relref = function() {
>>             // clean up
>>             activated = false;
>>             root.Release1 -= relref;
>>         }
>>         root.Release1 += relref;
>>     }
>> }
>
> Since the reference is only created in the Press1 trap, will it still 
> be around to be removed in the Release1 trap?

Yes. JavaScript "remembers" the entire scope chain. So Press1's scope 
chain will still be around when Release2 is called. You might want to 
stick a "var relref" in there somewhere so if you do this in multiple 
functions they don't clobber each other's relrefs. (Remember, writes to 
undeclared variables end up working their way all the way up to the box 
where the become box properties.)

-Brian