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?
高洛峰2017-05-18 11:05:11
The
addEventListener
的第二个参数可以传一个对象,当事件触发时,这个对象的handleEvent
method is called like this:
document.body.addEventListener(
'click',
{
handleEvent: function() {
alert('body clicked');
}
},
false);
Reference:
handleEvent of the second parameter of addEventListener
http://peter.michaux.ca/artic...
MDN
習慣沉默2017-05-18 11:05:11
Are you sure this addEventListener is the native JS window.addEventListener?