Using this in event handler in strict javascript?

前端 未结 3 1858
别那么骄傲
别那么骄傲 2021-01-25 19:49

Suppose you have a routine like the following to wire up click event handlers

getElements(\".board>div\").forEach(function(elem){
  elem.addEventListener(\"cl         


        
3条回答
  •  轮回少年
    2021-01-25 20:21

    Why don't you just bind the function with the object?

    getElements(".board>div").forEach(function(elem){
      elem.addEventListener("click", handleClick.bind(elem));
    });
    

提交回复
热议问题