I pass an anonymous function-A as parameter to another function-B. Even after I call B.destroy, function A exists and gets executed, it even as an live anonymous scope to itself. I found this unexpected behavior through debugger. Following is the code.
var builder.record.verifyExplorer = new builder.VerifyExplorer(
window.bridge.getRecordingWindow(),
builder.getScript().seleniumVersion,
function(step) {
builder.getScript().addStep(step);
builder.stepdisplay.update();
// Don't immediately stop: this would cause the listener that prevents the click from
// actually activating the selected element to be detached prematurely.
setTimeout(function() { builder.record.stopVerifyExploring(); }, 1);
window.bridge.focusRecorderWindow();
}
);
I destroy the above function by defining stopVerifyExploring to be
builder.record.stopVerifyExploring = function() {
builder.record.verifyExploring = false;
builder.record.verifyExplorer.destroy();
builder.record.verifyExplorer = null;
builder.record.continueRecording();
};
Even after verifyExplorer.destroy is called function(step) is live in anonymous scope and gets executed and does all unwanted things.
I have this weird behavior by replacing
jQuery(frame.document).bind(l, {}, ae.listeners[l], true);
with
frame.document.addEventListener(l,ae.listeners[l],true);
in verifyExplorer. How does me changing the above piece code lead to unexpected behavior?
All this is part of me fixing a bug in an open source project sebuilder. Also related post