I have an event, and I want to add additional parameters to the named function. I tried following two things:
myDiv.addEventListener('click', evt.call(this, event, 'hello'));
And
myDiv.addEventListener('click', evt(event, 'hello'));
And the problem with both of them, is they get called right away, and don't get called when you click myDiv, i.e. when it's supposed to get called.
How can I add additional parameters to the named function event?
JSFiddle
console.clear();
var myDiv = document.getElementById('myDiv');
function evt(event, param1) {
console.log(event + ' and ' + param1)
}
myDiv.addEventListener('click', evt.call(this, event, 'hello'));
#myDiv {
width: 200px;
height: 200px;
background-color: green;
}
<div id="myDiv"></div>
bind?Function#bindthis. I just want to add more parameters to the event functionbindwas to just add thethis. Thanks! Which is better, performance wise, between using an anonymous wrapper thenreturn evt.call(this, event, 'hello');, or creating a variable withbind?this. It just depends on if you needthisto be equal to something and if this is a performance critical section of code.