Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event Listener with anonymous function

It's written on the MDN that

If you want to pass parameters to the listener function, you may use an anonymous function.

After some experimentation I figured out when I try to register an event listener with a one-parameter function like this (without an anonymous function)

target.addEventListener(type, doSomething(parameter));

the listener function executes even when an event didn't happen, but when I wrap it up in an anonymous function

target.addEventListener(type, function () {doSomething(parameter);});

everything goes as expected.

Why does such behavior take place? I guess it is somehow connected with closures.

like image 786
Anton Timofeev Avatar asked Feb 19 '16 16:02

Anton Timofeev


People also ask

How do you use removeEventListener with anonymous function?

To call removeEventListener with an anonymous function with JavaScript, we assign the anonymous event listener function to a variable and call addEventListener and removeEventListener with the variable. const onScroll = () => { //... }; document. body.

Can event listener be in a function?

Event Listener BasicsTo set up an event listener you just need to have a variable that references an element and then call the addEventListener function on that element. This function takes a minimum of two parameters. The first parameter is just a string which is the name of the event to listen to.

How do you define anonymous function?

In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.

How do I get rid of anonymous event listener?

Strictly speaking you can't remove an anonymous event listener unless you store a reference to the function. Since the goal of using an anonymous function is presumably not to create a new variable, you could instead store the reference in the element itself: element. addEventListener('click',element.

How to remove an anonymous function from an event listener?

I personally use a variable to store the <target> and declare the function outside of the event listener invocation eg: Then to remove the listener: Not the top recommendation you will receive but to remove anonymous functions the only solution I have found useful is to remove then replace the HTML element.

How do I remove an event listener in JavaScript?

You can easily remove an event listener by using the removeEventListener () method. The first parameter is the type of the event (like " click " or " mousedown " or any other HTML DOM Event .) The second parameter is the function we want to call when the event occurs.

How do I add an event listener to a window object?

Add an Event Handler to the window Object The addEventListener () method allows you to add event listeners on any HTML DOM object such as HTML elements, the HTML document, the window object, or other objects that support events, like the xmlHttpRequest object.

What is eventlistener in Salesforce?

The EventListener interface represents an object that can handle an event dispatched by an EventTarget object. Note: Due to the need for compatibility with legacy content, EventListener accepts both a function and an object with a handleEvent () property function.


1 Answers

When defining the handler function like so

target.addEventListener(type, doSomething(parameter));

You are passing the function's return value as handler. For example consider this function:

function doSomething(event) {
    return 'foo';
}

Now, the function gets executed immediatly, before the event has happened, and you are basically just passing this as handler:

target.addEventListener(type, 'foo');

That can't work.

The second example

target.addEventListener(type, function () {doSomething(parameter);});

correctly passes a function as reference, without having it executed before the event occurred.

like image 133
baao Avatar answered Sep 28 '22 10:09

baao