JavaScript this Keyword Explained Simply
ฝัง
- เผยแพร่เมื่อ 28 ก.ย. 2024
- In most cases, the value of 'this' is determined by how a function is called. 'this' usually refers to the object that is calling the current function. If a function, foo(), is called from the global scope ("foo()"), then 'this' will refer to the global object - the Window object. But if foo() is called from inside the scope of another object ("devsage.foo()"), then 'this' will refer to the object that is calling foo() - which is the 'devsage' object. Arrow functions don't provide their own 'this' binding so it inherits the 'this' value of the enclosing function.
📚Materials/References:
MDN Web Docs: developer.mozi...
🌎 Find Me Here:
Twitter: / realdevsage
Discord: / discord
Ebooks: payhip.com/dev...
Merch: cottonbureau.c...
💙 Twitter: twitter.com/realDevSage
📙 Ebooks: payhip.com/devsage
💥 Discord: discord.gg/BP8wPv6raA
👕 Merch: cottonbureau.com/people/devsage
Seriously great explanation! Came here for more clarification on it after The Odin Project lesson Objects and Object Constructors
Same haha
Amazing explanation, much more concise than articles I've read.
Glad it helped!
Thanks a lot. One of the clearest explanations of `this`. 🔥
No problem, Aruna!
Bro, that arrow function confused me while reading an article T - T. Thanks man!
Happy to help!
i really want to say thank you because your simple explanation made me get it finally
Is it possible to import any module without writing the module type and export it
Great explanation, thank you!
I subscribed because i was able to understand "this"
Thanks!
was very helpful, thanks ..
You're welcome!
Thanks!
this is awesome
Thank you
'This' references to the object that calling the function.
This simple sentence cleared my mind after reading a lot of stuff about 'this'. Your explanation is amazing!!
So, could we say that using inline functions with the `this` keyword is discouraged (bad practice) since it would require it being nested within a normal function?
Meaning that instead of using an inline and nesting it, It's better to simply just use a normal function? Please correct me if I am mistaken! :)
Correct.
Very clean explanation, thanks for that!
No problem!
I am okey with this topic but, i have a question. When i use this keyword on a object literal like 'fullName : this' it says it is a global object and this kind of little things confuses me. Thank you.
Very nicely explained. Thanks
No problem!
Appreciate all your useful vids. They've substantially benefited me in the first couple months I started out.
Brilliant explaination.
I've been struggling to fully comprehend this; your video really helped me out. Looking forward to checking out more of your vids ;)
Very clear explanation ..
9:53 Could you just use an arrow function with the forEach method/function to solve the "this" problem?
Yes!
Finally someone that doesn't speak overly fast
best video on this topic! ty.
No problem!
Very good and instructive explanation
Great explanation, thanks!
a big thumb up for you, thank you!
you are a legend
on forEach (8:02) when this refers to window object why dont we use arrow function for ex: this.hobbies.forEach((hobby)=>{ ( console.log( this.firstName )} ));
You can, i guess he just wanted us to learn how to fix it without changing the callback function type.
Right , but its more interesting to do that way for the sake of concept and learning isnt ?
best tutorial on "this" subject
Thanks!