I'm trying to understand the difference beetween functions arrow and functions in javascript, with the help of some website, and from what I seemed to understand, it's the this binding that change in the case
So I understood that in function arrow, this is binded to the closest non-arrow function parent this and in regular function, this is binded from the closest this
So with that in mind, I was doing some test in the browser console and came up with this:
test = {
test_name: "OK",
test_func: function() {
console.log(this.test_name);
(() => {
console.log(this.test_name);
function b() {
console.log(this.test_name);
}
b();
})();
}
}
test.test_func();
I was excepting to get
OK
OK
OK
but what I get is
OK
OK
undefined
And I can't understand why since I get 'OK' in the arrow function, why don't I get 'OK' in the b() function?
From w3schools:
The handling of this is also different in arrow functions compared to regular functions.
In short, with arrow functions there are no binding of this.
In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever.
With arrow functions the this keyword always represents the object that defined the arrow function.