I have seen come code which is related to addEventListener
function and this
keyword in JavaScript.
I know this
refers to the global object and addEventListener
registers the specified listener on the EventTarget it's called on.
var Something = function (element) {
this.name = 'Something Good';
this.onclick1 = function (event) {
console.log(this.name);
};
this.onclick2 = function (event) {
console.log(this.name);
};
element.addEventListener('click', this.onclick1, false);
element.addEventListener('click', this.onclick2.bind(this), false);
}
Here I just need to know how the last line of this above script works:
element.addEventListener('click', this.onclick2.bind(this), false);
I understand what bind
does -- I just need to know what do the two this
s represents in this.onclick2.bind(this)
. Do they represents element and event attributes, that is, is it acting like element.onclick2.bind(event)
?