|
| 1 | +--- |
| 2 | +chapter: 8 |
| 3 | +description: Understanding the `this` Keyword in JavaScript. |
| 4 | +--- |
| 5 | + |
| 6 | +## Understanding the `this` Keyword in JavaScript |
| 7 | + |
| 8 | +The `this` keyword in JavaScript refers to the object it belongs to. It has different values depending on where it is used: in a method, alone, in a function, in an event, etc. |
| 9 | + |
| 10 | +### `this` in Global Context |
| 11 | + |
| 12 | +In the global execution context (outside of any function), `this` refers to the global object, which is `window` in browsers. |
| 13 | + |
| 14 | +```javascript |
| 15 | +console.log(this); // Output: Window {...} |
| 16 | +``` |
| 17 | + |
| 18 | +### `this` in Object Methods |
| 19 | + |
| 20 | +When used in an object method, `this` refers to the object the method belongs to. |
| 21 | + |
| 22 | +```javascript |
| 23 | +const person = { |
| 24 | + firstName: "John", |
| 25 | + lastName: "Doe", |
| 26 | + fullName: function() { |
| 27 | + return `${this.firstName} ${this.lastName}`; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +console.log(person.fullName()); // Output: John Doe |
| 32 | +``` |
| 33 | + |
| 34 | +### `this` in Constructor Functions |
| 35 | + |
| 36 | +In a constructor function, `this` refers to the newly created instance. |
| 37 | + |
| 38 | +```javascript |
| 39 | +function Person(firstName, lastName) { |
| 40 | + this.firstName = firstName; |
| 41 | + this.lastName = lastName; |
| 42 | +} |
| 43 | + |
| 44 | +const person1 = new Person("Jane", "Smith"); |
| 45 | +console.log(person1.firstName); // Output: Jane |
| 46 | +``` |
| 47 | + |
| 48 | +### `this` in Arrow Functions |
| 49 | + |
| 50 | +Arrow functions do not have their own `this`. Instead, `this` is lexically inherited from the outer function where the arrow function is defined. |
| 51 | + |
| 52 | +```javascript |
| 53 | +const person = { |
| 54 | + firstName: "John", |
| 55 | + lastName: "Doe", |
| 56 | + fullName: function() { |
| 57 | + const getFullName = () => `${this.firstName} ${this.lastName}`; |
| 58 | + return getFullName(); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +console.log(person.fullName()); // Output: John Doe |
| 63 | +``` |
| 64 | + |
| 65 | +### `this` in Event Handlers |
| 66 | + |
| 67 | +In event handlers, `this` refers to the element that received the event. |
| 68 | + |
| 69 | +```html |
| 70 | +<button id="myButton">Click me</button> |
| 71 | +<script> |
| 72 | + document.getElementById("myButton").addEventListener("click", function() { |
| 73 | + console.log(this); // Output: <button id="myButton">Click me</button> |
| 74 | + }); |
| 75 | +</script> |
| 76 | +``` |
| 77 | + |
| 78 | +### Changing `this` with `call`, `apply`, and `bind` |
| 79 | + |
| 80 | +You can explicitly set the value of `this` using `call`, `apply`, and `bind`. |
| 81 | + |
| 82 | +#### `call` Method |
| 83 | + |
| 84 | +The `call` method calls a function with a given `this` value and arguments provided individually. |
| 85 | + |
| 86 | +```javascript |
| 87 | +function greet() { |
| 88 | + console.log(`Hello, ${this.name}`); |
| 89 | +} |
| 90 | + |
| 91 | +const person = { name: "Alice" }; |
| 92 | +greet.call(person); // Output: Hello, Alice |
| 93 | +``` |
| 94 | + |
| 95 | +#### `apply` Method |
| 96 | + |
| 97 | +The `apply` method calls a function with a given `this` value and arguments provided as an array. |
| 98 | + |
| 99 | +```javascript |
| 100 | +function greet(greeting) { |
| 101 | + console.log(`${greeting}, ${this.name}`); |
| 102 | +} |
| 103 | + |
| 104 | +const person = { name: "Bob" }; |
| 105 | +greet.apply(person, ["Hi"]); // Output: Hi, Bob |
| 106 | +``` |
| 107 | + |
| 108 | +#### `bind` Method |
| 109 | + |
| 110 | +The `bind` method creates a new function that, when called, has its `this` keyword set to the provided value. |
| 111 | + |
| 112 | +```javascript |
| 113 | +function greet() { |
| 114 | + console.log(`Hello, ${this.name}`); |
| 115 | +} |
| 116 | + |
| 117 | +const person = { name: "Charlie" }; |
| 118 | +const greetPerson = greet.bind(person); |
| 119 | +greetPerson(); // Output: Hello, Charlie |
| 120 | +``` |
| 121 | + |
| 122 | +### Conclusion |
| 123 | + |
| 124 | +Understanding the `this` keyword is crucial for writing effective JavaScript code. Its value depends on the context in which it is used, and it can be explicitly set using `call`, `apply`, and `bind`. |
0 commit comments