
This Keyword in JS
this keyword of javascript actually confuse a lots of junior and senior developer. So let me explain in the easist way possible. this behavior doen't depend upon where this is used but from where it is called. Understand this by a example. Example: let obj = { name : " Kush " , displayName (){ console . log ( `My name is ${ this . name } ` ); } } Now, when obj.displayName() is called we get output as : // My name is Kush. But what if i change this to something like this : let person=obj.displayName; person(); Now the output will be : // My name is undefined. So, this show us that the execution of this is more important then the place where this is defined. In non strict mode, this point to the global window object in browser and can be different in other runtimes. In strict mode, this point to the undefined. Arrow function: Arrow functions do not have their own this; they capture this from their lexical scope (where they were created). let obj = { name : " Kush " , personName :() => {
Continue reading on Dev.to
Opens in a new tab



