Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Remove eventlistener for anonymous functions with a reference

20 views
Skip to first unread message

TheRave

unread,
Oct 16, 2012, 3:38:27 PM10/16/12
to
Hello,

according my tests for removing an eventlistener definied for a anonymous function i found a solution, but i doesn't know why this works.

var myExtension = {
init: ....

},
onPageLoad: function(aEvent) {

var test = "test";

doc.addEventListener("textAreaEvent", afunc = function(evt){onChangeTextArea(evt, data);}, false);

....
},

onPageUnload: function(aEvent) {
//alert(test); <---- of course unknown

alert(afunc); <----Why is afunc known here?

aEvent.originalTarget.removeEventListener("textAreaEvent", afunc, false);

Can somebody give me an explanation why this workarround works and afunc is well known for onPageUnload? There is no global var afunc defined it was only done in addeventlistener.

Godmar Back

unread,
Oct 16, 2012, 3:43:00 PM10/16/12
to TheRave, dev-ext...@lists.mozilla.org
On Tue, Oct 16, 2012 at 3:38 PM, TheRave <micha....@arcor.de> wrote:

> Hello,
>
> according my tests for removing an eventlistener definied for a anonymous
> function i found a solution, but i doesn't know why this works.
>
> var myExtension = {
> init: ....
>
> },
> onPageLoad: function(aEvent) {
>
> var test = "test";
>
> doc.addEventListener("textAreaEvent", afunc =
> function(evt){onChangeTextArea(evt, data);}, false);
>
>
Unless you have a 'var afunc' in a smaller scope, this sets 'afunc' in the
global scope.
So no surprise here.




> ....
> },
>
> onPageUnload: function(aEvent) {
> //alert(test); <---- of course unknown
>
> alert(afunc); <----Why is afunc known here?
>
> aEvent.originalTarget.removeEventListener("textAreaEvent",
> afunc, false);
>
> Can somebody give me an explanation why this workarround works and afunc
> is well known for onPageUnload? There is no global var afunc defined it was
> only done in addeventlistener.
> _______________________________________________
> dev-extensions mailing list
> dev-ext...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-extensions
>

TheRave

unread,
Oct 16, 2012, 3:52:20 PM10/16/12
to
Yes. Now i can remember that vars defined without "var" are in global scope. That's it! Thank you.
0 new messages