EXECUTION CONTEXT
- Variable environment
- Scope chain
- this keyword
- this keyword/variable: Special variable that is created for every execution context (every function). Takes the value of (points to) the "owner" of the function in which the this keyword is used.
- this is NOT static. It depends on how the function is called, and its value is only assigned when the function is actually called.
Method
this = <Object that is calling the method>
const sunny = {
name: 'Sunny',
year: 1988,
calcAge: function () {
return 2022 - this.year;
}
};
sunny.calcAge();
calcAge is method
this -> sunny
this.year -> 1988 가리킴
Way better than using sunny.year!
Simple function call
this = undefined
-> In strict mode! Otherwise: window (in the browser)
Arrow functions
this = <this of surrounding function (lexical this)>
-> Arrow functions do not own this keyword ⭐️⭐️⭐️
Event listener
-> this = <DOM element that the handler is attached to>
this does NOT point to the function itself, and also NOT the its variable environment!
'Programming > JavaScript' 카테고리의 다른 글
Primitives vs. Objects (Primitive vs. Reference Types) (0) | 2022.11.22 |
---|---|
Regular Functions vs. Arrow Functions (0) | 2022.11.21 |
The this Keyword in Practice (0) | 2022.11.21 |
Variable Environment: Hoisting and The TDZ (0) | 2022.11.20 |
Hoisting and The TDZ in Practice (0) | 2022.11.20 |