|
4 | 4 | // - In JavaScript, the this keyword refers to an current context.
|
5 | 5 |
|
6 | 6 | // Note
|
7 |
| -// - The this keyword refers to different objects depending on how it is used: |
8 |
| -// 1. Alone -> Global object |
9 |
| -// 2. In a object method -> object |
10 |
| -// 3. In a function -> Global object |
11 |
| -// 4. In a function with strict mode ("use strict") -> undefined |
12 |
| -// - In an event, this refers to the element that received the event. |
13 |
| -// - Methods like call(), apply(), and bind() can refer this to any object. |
14 |
| - |
15 |
| -// 1. Alone -> Global object |
| 7 | +// The this keyword refers to different objects depending on how it is used: |
| 8 | +// 5. Arrow function and `this` keyword (Not Recommended) -> See the code example below |
| 9 | +// 6. In a function with strict mode ("use strict") -> undefined |
| 10 | +// TODO: In an event, this refers to the element that received the event. |
| 11 | +// TODO: Methods like call(), apply(), and bind() can refer this to any object. |
| 12 | + |
| 13 | +// 1. this -> Alone -> Global object |
16 | 14 | console.log(this);
|
17 | 15 |
|
18 |
| -// 2. In a object method -> object |
| 16 | +// Regular function & `this` keyword |
| 17 | + |
| 18 | +// 2. this -> In object method (regular function) -> Current Object |
19 | 19 | const object = {
|
20 |
| - key1: "Value1", |
21 |
| - key2: "Value2", |
22 |
| - key3: function () { |
23 |
| - console.log(this.key1); |
| 20 | + key: "value", |
| 21 | + method: function () { |
| 22 | + console.log(this); |
| 23 | + console.log(this.key); |
24 | 24 | },
|
25 | 25 | };
|
26 |
| -console.log(object.key3()); |
| 26 | +console.log(object.method()); |
27 | 27 |
|
28 |
| -// 3. In a function -> Global object |
29 |
| -function iAmFunction() { |
| 28 | +// 3. this -> In regular function -> Global object |
| 29 | +function regularFunction() { |
30 | 30 | return console.log(this);
|
31 | 31 | }
|
| 32 | +console.log(regularFunction()); |
32 | 33 |
|
33 |
| -console.log(iAmFunction()); |
34 |
| - |
35 |
| -// 4. Function inside function -> Global object |
36 |
| -function iAmOutsideFunction() { |
37 |
| - function iAmInsideFunction() { |
| 34 | +// 4. this -> Function inside function -> Global object |
| 35 | +function outerRegularFunction() { |
| 36 | + function innerRegularFunction() { |
38 | 37 | return console.log(this);
|
39 | 38 | }
|
40 |
| - console.log(iAmInsideFunction()); |
| 39 | + console.log(innerRegularFunction()); |
41 | 40 | }
|
42 |
| -console.log(iAmOutsideFunction()); |
| 41 | +console.log(outerRegularFunction()); |
43 | 42 |
|
44 |
| -// 5. Arrow function and this keyword |
45 |
| -// - Arrow functions have a different behavior with this. They inherit the this value from the enclosing scope where they are defined, not from how they are called. |
| 43 | +// Arrow function & `this` keyword |
| 44 | + |
| 45 | +// Note |
| 46 | +// - Never use the arrow function inside method or constructor. |
| 47 | + |
| 48 | +// 5. this -> In object method (arrow function) -> Global object |
| 49 | +const object1 = { |
| 50 | + key: "value", |
| 51 | + method: () => { |
| 52 | + return console.log(this); |
| 53 | + }, |
| 54 | +}; |
| 55 | +console.log(object1.method()); |
46 | 56 |
|
47 |
| -// Defined in the Global Scope -> Global object |
| 57 | +// 6. this -> In arrow function -> Global object |
48 | 58 | const arrowFunction = () => {
|
49 | 59 | return console.log(this);
|
50 | 60 | };
|
51 | 61 | console.log(arrowFunction());
|
52 | 62 |
|
53 |
| -// Defined Inside a Function or Object |
54 |
| -const object1 = { |
55 |
| - key1: "value1", |
56 |
| - key2: "value2", |
57 |
| - key3: () => { |
| 63 | +// 7. this -> Arrow function inside arrow function -> Global object |
| 64 | +const outerArrowFunction = () => { |
| 65 | + const innerArrowFunction = () => { |
58 | 66 | console.log(this);
|
59 |
| - }, |
| 67 | + }; |
| 68 | + console.log(innerArrowFunction()); |
60 | 69 | };
|
| 70 | +console.log(outerArrowFunction()); |
| 71 | + |
| 72 | +// If you are thinking that. If `this` keyword is always refer to global object. Why can't we just rap the object around it. Then it will sure stop referring to global object. |
61 | 73 |
|
62 |
| -console.log(object1.key3()); |
| 74 | +const outerObject = { |
| 75 | + key1: "value", |
| 76 | + innerObject: { |
| 77 | + arrowFunction: () => { |
| 78 | + console.log(this.key1); |
| 79 | + }, |
| 80 | + }, |
| 81 | +}; |
| 82 | +console.log(outerObject.innerObject.arrowFunction()); |
| 83 | +// `this` just doesn't care. It won't show the key1's -> va lue. |
0 commit comments