Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion closures, what are they good for?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Richard Cornford  
View profile  
 More options Apr 24 2003, 1:11 pm
Newsgroups: comp.lang.javascript
From: "Richard Cornford" <Rich...@litotes.demon.co.uk>
Date: Thu, 24 Apr 2003 18:10:56 +0100
Local: Thurs, Apr 24 2003 1:10 pm
Subject: Re: closures, what are they good for?
Richard Cornford wrote in message ...

<snip>

>.... It seems that it would often be a good idea to include, and
>use, explicit 'destroy' functions with DHTML and ActiveX based
>JavaScript objects that use closures. To free up any references
>from JavaScript objects before/as a page unloads.

That of course requires that multiple objects or classes have methods
called by the window.onunload event. Not a problem if the browser
supports addEventListener. However, as the subject of this thread is
uses that closures may be put to, this closure offering from Yep might
of interest:-

<URL:
http://groups.google.com/groups?selm=d2d855ea.0211180417.44bcd1d4%40post
ing.google.com >

function _E(obj, evt, func){
  if(obj[evt]) {
    obj[evt]=function(_f,_g){
      return function(){
        _f.apply(this,arguments);
        _g.apply(this,arguments);
      };
    }(obj[evt],func);
  }else obj[evt]=func;

}

Unfortunately it might also require a Function.prototype.apply emulation
on older IE versions, but if that is only for use with event handlers it
could be a very simple emulation. Yep has also kept this function very
general and is making no consideration of return values. That would not
be a problem when used with window.onunload as not return values are
relevant. The - apply - method would probably also not be necessary with
the window.onunload event as it would not matter whether the _f and _g
functions where executed in the global context because that is the
window anyway.

So, maybe (untested):-

function _E(func){
    if(window.onunload){
        window.onunload = function(_f, _g){
            return function(ev){
                _f(ev);
                _g(ev);
            };
        }(window.onunload, func);
    }else window.onunload = func;

}

Thus (and assuming that any HTML defined onunload handlers are already
set up and no other script is going to directly assign window.onunload)
each instance can arrange that its own destroy/finalize method is called
with the onunload event.

Based on the recent discussion of JavaScript static (and particularly,
private static) members, I think that it would be possible to have the
class track its own instances and have onunload call a class method that
could destroy/finalize all of its instances at once. But that would only
be worth while if it was expected that there be a lot of instances.

Richard.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.