Before I ask my question I'll set some context.
I'm still learning and did an exercise today and I had to use a traditional function with the 'this' keyword.
Using the example below with a console.log I am able to retrieve the value I want eg input.value.
let inputs = document.querySelectorAll(".controls input")
let input = document.querySelector("input")
const handleUpdate(){
console.log(this.value) \\ the output is the value of input.value eg 25
}
inputs.forEach(item=>item.addEventListener("mousemove", handleUpdate))
inputs.forEach(item=>item.addEventListener("change", handleUpdate))
However, if I use an ES6 function with a fat arrow then it doesn't work and get 'undefined'.
After furiously reading articles, I understand 'this' would always point to the parent object eg. global object(window)(please correct me if I'm wrong. So an ES6 function would always return 'undefined' in this instance. If I change 'this.value' to 'input.value' it works ok, see below.
let inputs = document.querySelectorAll(".controls input")
let input = document.querySelector("input")
const handleUpdate=()=>{
console.log(this.value) \\ undefined
console.log(input.value) \\ the output is the value of input.value eg 25
}
inputs.forEach(item=>item.addEventListener("mousemove", handleUpdate))
inputs.forEach(item=>item.addEventListener("change", handleUpdate))
My question is should I be using the first approach which is a traditional function with the 'this' keyword or the approach with ES6 syntax(which just means I can't use 'this' keyword). Does it matter which I use?
Thanks for any help with this.