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.

4

2 回答 2

2

是的,这将创建一个循环引用。

但是,它不会引起任何问题。
现代 Javascript 垃圾收集器可以很好地处理循环引用。(在 IE6 中,DOM 和用户对象之间的引用除外)

于 2012-07-04T15:11:29.590 回答
0

如果s所有引用它的东西要么离开可达范围要么被手动删除,那么它将被垃圾收集。如果您删除sstatement.executeAsync仍然引用回调,则对的引用也me将保留。

delete s;
s; //undefined

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

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

如果您只是单独清除每个处理程序而不是语句对象,那也可以。假设您可以单独访问每个回调。

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

这也取决于 executeAsync 是如何实现的。如果内部实现没有将回调存储在最终回调之后的范围内,那么它将在您删除后被清理s

于 2012-07-04T15:28:11.460 回答