0

I read this on w3schools that:- https://www.w3schools.com/js/js_arrow_function.asp
"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."

var name = "ABC";
var obj = 
{
     name: "abc",
     func: () => {
        console.log(this.name);
     }
};
obj.func();

So here, 'this' in the arrow function should be representing 'obj' object, but the output is "ABC", whereas according to me it should be "abc", there should be some concept that I am not aware of right now?

vaibhav s
  • 65
  • 5
  • w3schools is wrong as usual - strongly consider using a more reliable site to get your information from. `this` inside an arrow function is inherited from the outer scope. Here, the outer scope is the top level, so `this` is `window` – CertainPerformance Feb 08 '20 at 06:24
  • I really wish i knew a good tutorial for JS. I only know, that w3schools is absolutely terrible, yet google still always puts it as #1. [Meta topic about w3schools](https://meta.stackoverflow.com/questions/280478/why-not-w3schools-com). – ASDFGerte Feb 08 '20 at 06:25
  • If they wrote `With arrow functions, the 'this' keyword always represents the **class** that defined the arrow function` then theyd be okay. Always stick to [mdn](https://developer.mozilla.org/), w3s was awful even before mdn became readable (i used [dottoro](http://help.dottoro.com/) back then) – Hashbrown Feb 08 '20 at 06:28

0 Answers0