The this Keyword
HTML-код
- Опубликовано: 17 сен 2024
- The ‘this’ keyword is extremely important concept to understand in JavaScript. Many beginners find it especially difficult to understand ‘this’ keyword. But don’t worry, it’s actually not that hard if you know exactly how the ‘this’ keyword works.
The ‘this’ keyword or ‘this’ variable is basically a special variable that is created for every execution context and therefore any function.
In fact, we learned before, that it is one of the three components of any execution context along with the variable object and scope chain that we already covered.
In general terms, the ‘this’ keyword will always take the value of the owner of the function in which the ‘this’ keyword is used. We also say that it points to the owner of the function.
Now, before I explain how the value of this keyword is determined, keep in mind, the value of this keyword is not static. So, its not always the same. It depends on how the function is actually called. The value of ‘this’ variable is only assigned when the function is actually called i.e. at runtime.
The value of this can be determined depending on how the function is called. In this lecture, lets understand how value of this variable is determined.
I have a question why you don’t have one million sub man your content is so cool and amazing keep up man thank you very much
Your explanation is really great, i m so lucky to find such a great channel ❤️
Great explanation! I am glad I found your channel. Thanks!
Thanks for watching!
Very well explained, especially that this keyword in an arrow function uses the lexical this scope!
I finally understand this. Thank you!
how it behave when we use arrow function as method inside objection , please cover on this
const address={
name:"Peter",
age: 26,
something: () => {
return this.innerHeight;
}
}
console.log(address.something())
why this arrow function is referring 'this' as window object, when its parent scope is the address object? Can you please explain
ok, i got this. as per your definition, arrow function takes the scope of its parent, now the scope of its parent(address object) is global scope as address object can be used in the global code, that could be the reason for this behaivour, Please correct me if my understanding is wrong
If we make calcAge as a arrow function & we call like John.calcAge() then what will 'this' point to ? John object or window object?...
If you are defining calcAge function inside John object, and if the calcAge function is an arrow function, then the this keyword inside that arrow function will point to global window object
But, if the arrow function is defined inside a regular function, and that regular function is defined inside John object, in that case, this keyword in regular function will point to John object. And in turn, this keyword inside arrow function will also point to John object. Why? Because in arrow function, this keyword points to its immidiate sorrounding this keyword. In this case the immidiate sorrounding this is pointing to John object.
Hope it make sense.
@@procademy thanks for clarification...I am glad and feel blessed to found such knowledgeable and informative channel on RUclips. Every developer should watch all J's watchlist.so beautifully explained. Thanks again for sharing with us.❤️🧡💛
Please share this channel with others if you like my videos...🤝🤝
@@procademy sure