Is the anonymous function required for the event callback?

Currently my code works when I pass the anonymous CONTAINING function of my method call in the callback, but not when I try to call my own method directly in the callback ... IE:

var that = this;

// THIS WORKS:
gt.pubads().addEventListener('slotRenderEnded', function(event) {
    that.onSlotRenderEnded(event);
});

// THIS DOES NOT. why? 
gt.pubads().addEventListener('slotRenderEnded', that.onSlotRenderEnded(event));

      

I'm trying to figure out why my method call needs to be wrapped in an anonymous function before it fires. I would like to be able to just shorten the code and call the custom method directly and not through the anonymous one.

+3


source to share


4 answers


// THIS DOES NOT. why? 
gt.pubads().addEventListener('slotRenderEnded', that.onSlotRenderEnded(event));

      

In this case, your second argument is actually the result of a call that.onSlotRenderEnded(event)

(possibly not a function), so it doesn't work.

And no. It doesn't require it to be an anonymous function. Just a function.

People usually use them because these action functions (which will react to events) are most often required only at that particular moment and nowhere else.

But you can still do:

gt.pubads().addEventListener('slotRenderEnded', function(event) {
    that.onSlotRenderEnded(event);
});

      

or

function someName(event) {
    that.onSlotRenderEnded(event);
}
gt.pubads().addEventListener('slotRenderEnded', someName);

      



or

var someName = function (event) {
    that.onSlotRenderEnded(event);
}
gt.pubads().addEventListener('slotRenderEnded', someName);

      

Or, even in this case, since all you do is delegate,

gt.pubads().addEventListener('slotRenderEnded', that.onSlotRenderEnded);

      

Everyone should behave the same *.

The only possible difference between them is the value this

, which can only be determined at the time of the actual call. In this case, if you want to be sure of the value this

, you can either use anonymous (and then not use a reference this

, but some other variable, for example that

, you are already using in the question), or you can resort to $.proxy()

, useful function, useful for these situations, the result is something like:

gt.pubads().addEventListener('slotRenderEnded', $.proxy(that, 'onSlotRenderEnded'));

      

(* Of course, unless you have other variables with sameName

in scope, etc.)

+5


source


It doesn't need to be wrapped with an anonymous function. Rather, pass the function definition without doing (using a parenthesis) like

gt.pubads().addEventListener('slotRenderEnded', that.onSlotRenderEnded);

      



When passing in a callback, you have two options. Either you are passing an anonymous function or you are passing a function definition. In both cases, the object event

will be automatically passed as the first argument to the callback.

+2


source


gt.pubads().addEventListener('slotRenderEnded', that.onSlotRenderEnded(event));

      

Here you are calling the method that.onSlotRenderEnded

with undefined since the event is undefined. And the result of this method is passed toaddEventListener

0


source


By adding parentheses and passing events, you call your handler right away. Drop those and you should be fine.

So instead of

that.onSlotRenderEvent(event)

      

Just do

that.onSlotRenderEvent

      

0


source







All Articles