Arrow functions are bound to the current context.
What does it mean?
This means that inside the function the value of this
will be the same as at the moment of creation.
Consider the examples from the question:
(function(){ const requestBtn = d.getElementById('reqestInfo'); requestBtn.addEventListener('click', ()=>{ console.log(this) }, false) })()
This code is executed inside a self-invoking function. Inside this function, this
-> global object, in this case, window
. Hence inside the handler, this this
persist.
Second example:
(function(){ const requestBtn = d.getElementById('reqestInfo'); requestBtn.addEventListener('click', ()=>{ console.log(this) }.bind(requestBtn), false) })()
There is really a syntax error, to fix it you need to wrap the switch function in parentheses
(()=>{ console.log(this) }).bind(requestBtn)
But this does not help, since the switch functions cannot change the context obtained during creation.
Now, why does the last example work:
(function(){ const requestBtn = d.getElementById('reqestInfo'); requestBtn.addEventListener('click', function(){ console.log(this) }, false) })()
Because, when adding a handler through the addEventListener function, the handler is called in the context of the added element. That is, this is essentially normal behavior, when this
inside a handler refers to an element, and all dancings with a change of context are usually due to the fact that you wanted to see this
as a handler, not an element, but some third-party object.