trying to learn the 'this' key word inside an arrow function properly.
after reading and watching some videos i understood that with the regular function the 'this' key word will be defined whenever the function is invoked.
and 'this' inside the arrow function will be defined according to the value of 'this' wherever you build that function.
so i opened the console and played with two objects and the two functions.
i did this:
const reg = {
reg1: "reg1",
reg2: {
reg3: 'reg3',
regFunc: function(){console.log(this)}
}
}
const arrow = {
arrow1: "arrow1",
arrow2: {
arrow3: 'arrow3',
arrowFunc: () => {console.log(this)}
}
}
reg.reg2.regFunc()
VM712:5 {reg3: "reg3", regFunc: ƒ}
arrow.arrow2.arrowFunc()
VM712:13 Window {parent: Window, opener: null, top: Window, length: 1, frames: Window, …}
for the reg Object i get it because it was invoked inside the reg2 object which it shows me.
but for the arrow Object the this keyword in the arrow function was created inside the arrow2 object inside arrow.
so why isn't the value of it in the arrow object arrow2? why is it the global window?