Recently I was looking at the source code implementation of some libraries, and I discovered that this was passed into the second parameter of addEventListener
. I felt confused, so I came up to ask questions. The following code is what I simply simplified and passed the test
<script>
this.onclick=function(){
console.log('onclick');
}
app.addEventListener('click',this);
</script>
Why can events be bound in this way?
The
addEventListener
的第二个参数可以传一个对象,当事件触发时,这个对象的handleEvent
method is called like this:Reference:
handleEvent of the second parameter of addEventListener
http://peter.michaux.ca/artic...
MDN
Are you sure this addEventListener is the native JS window.addEventListener?