I am converting one of my old javascript code to ECMA6. Getting problem in passing "this" in the callback to the "addEventListener" method.
Here is my HTML,
<p><span id="temperature"></span><span id="degname">C</span></p>
In My Javascript I am making this span element a clickable element. In my old code "this" is passed nicely in the callback and it is pointing to the "span" element but in the ECMA6 code "This" is pointing to the "window" object.
I researched about the issue. There is a option of using bind function to the callback in ECMA6 to pass the "this" pointer but I want to use the anonymous callback.
Could anyone please tell how can I pass the parent object(here "span" element) "this" pointer to the anonymous callback ?
Here is my old code,
document.getElementById("degname").addEventListener("click",function(){
//alert("hello fahrenhite");
console.log("the value of this = "+this); **//"this" is here the span element**
var name = this.innerHTML;
console.log("name is : "+name); //name is giving right value
});
and here is the ECMA6 :
document.getElementById("degname").addEventListener("click",() => {
console.log("The value of this = "+this); **//"this" is here is windows object**
let name = this.innerHTML; **//name is here undefined**
});