How to pass arguments to addEventListener listener function?

前端 未结 30 2274
谎友^
谎友^ 2020-11-21 23:56

The situation is somewhat like-

var someVar = some_other_function();
someObj.addEventListener(\"click\", function(){
    some_function(someVar);
}, false);
<         


        
30条回答
  •  不思量自难忘°
    2020-11-22 00:55

    You can just bind all necessary arguments with 'bind':

    root.addEventListener('click', myPrettyHandler.bind(null, event, arg1, ... ));
    

    In this way you'll always get the event, arg1, and other stuff passed to myPrettyHandler.

    http://passy.svbtle.com/partial-application-in-javascript-using-bind

提交回复
热议问题