1

In Determing if a SQL select returns an empty set asynchronously? I ended up trying to use an object's method's as an event handler. Using the normal "this" of the object doesn't correspond to the method's object in the context of an event handler (why, not sure, beyond my current simplistic understanding of javascript, my guess is due to scoping of variable names?).

As I mentioned in my previous Q, from http://w3future.com/html/stories/callbacks.xml, their solution is to basically have each object provide a "var me = this" variable. My question is, does this create a circular reference that will prevent the object from being garbage collected?

If so, is there a better way to accomplish the task?

thanks.

Community
  • 1
  • 1
spotter
  • 1,178
  • 1
  • 9
  • 19

2 Answers2

2

Yes, that will create a circular reference.

However, it won't cause any problems.
Modern Javascript garbage collectors can handle circular references perfectly well. (except, in IE6, for references crossing between DOM and user objects)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

If s and everything that references it either leaves reachable scope or is manually deleted, then it will get garbage collected. If you delete s but statement.executeAsync still has reference to the callbacks, the references to me will stay around too.

delete s;
s; //undefined

//This will still contain the function reference
statement.executeAsync.handleResult;

delete statement; //Now s and me should be garbage collected

If you just clear each handler individually instead of the statement object, that would work too. Assuming you can reach each callback individually.

delete statement.executeAsync.handleResult;
delete statement.executeAsync.handleError;
delete statement.executeAsync.handleCompletion;

This also depends on how executeAsync is implemented. If the internal implementation doesn't store the callbacks in a scope that lives past the final callback, then it would get cleaned up with you deleted s

Tom Chandler
  • 642
  • 3
  • 9