|
1 | | -# **Are you looking for `this`? '🙄 ** |
| 1 | +# **Are you looking for `this`? 🙄** |
2 | 2 |
|
3 | | -[Home](../README.md) / Are you looking for `this`? '🙄 |
| 3 | +[Home](../README.md) / Are you looking for `this`? 🙄 |
4 | 4 |
|
5 | 5 | --- |
6 | 6 |
|
| 7 | +## **What is `this` 😥** |
| 8 | + |
| 9 | +In JavaScript `this` refers to the current object that is executing the current function. It has different values depending on where it is used. |
| 10 | + |
| 11 | +Read more or about objects [here](./objects.md). |
| 12 | + |
| 13 | +## The `this` reference 😳 |
| 14 | + |
| 15 | +✅ If the function is inside an Object then `this` refers to the that object. |
| 16 | + |
| 17 | +Example: |
| 18 | + |
| 19 | +```js |
| 20 | +const cookie = { |
| 21 | + flavour: "chocolate", |
| 22 | + eat() { |
| 23 | + console.log(`I am a ${this.flavour} 🍪 and I was eaten!`); |
| 24 | + }, |
| 25 | +}; |
| 26 | + |
| 27 | +Cookie.eat(); |
| 28 | + |
| 29 | +// This is another way of writing the above code. |
| 30 | +const cookie = { |
| 31 | + flavour: "chocolate", |
| 32 | + eat: function () { |
| 33 | + console.log(`I am a ${this.flavour} 🍪 and I was eaten!`); |
| 34 | + }, |
| 35 | +}; |
| 36 | + |
| 37 | +// Both the codes are same. |
| 38 | +``` |
| 39 | + |
| 40 | +This will not work if we use it inside an [Arrow Function](./arrow-functions.md) or inside a nested function. |
| 41 | + |
| 42 | +Example |
| 43 | + |
| 44 | +```js |
| 45 | +// this will not work |
| 46 | +const cookie = { |
| 47 | + flavour: "chocolate", |
| 48 | + eat: () => { |
| 49 | + console.log(`I am a ${this.flavour} 🍪 and I was eaten!`); |
| 50 | + }, |
| 51 | +}; |
| 52 | + |
| 53 | +// OR |
| 54 | + |
| 55 | +const cookie = { |
| 56 | + flavours: ["chocolate", "stawberry", "vanilla"], |
| 57 | + eatAllCookies() { |
| 58 | + this.flavours.forEach(function (flavour) { |
| 59 | + // 👈 Anonymous nest function |
| 60 | + console.log(`I am ${flavour} 🍪 and I was eaten!`); // 👈 Will output "I am 'undefined' and I was eaten!" |
| 61 | + }); |
| 62 | + }, |
| 63 | +}; |
| 64 | +``` |
| 65 | + |
| 66 | +To bind the object we need to pass the `this` object as an argument to the `forEach()` function. |
| 67 | + |
| 68 | +```js |
| 69 | +const cookie = { |
| 70 | + flavours: ["chocolate", "stawberry", "vanilla"], |
| 71 | + eatAllCookies() { |
| 72 | + this.flavours.forEach(function (flavour) { |
| 73 | + console.log(`I am ${flavour} 🍪 and I was eaten!`); |
| 74 | + }, this); // 👈 here this refers to the current "flavours" array, which in turn is an object. |
| 75 | + }, |
| 76 | +}; |
| 77 | + |
| 78 | +// Works like a charm 😋 |
| 79 | +cookie.eatAllCookies(); |
| 80 | +``` |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +✅ If the function is a regular function then `this` refers to the `window` object in browser and the `global` object in node. |
| 85 | + |
| 86 | +Example: |
| 87 | + |
| 88 | +```js |
| 89 | +function eatCookie() { |
| 90 | + console.log(this); |
| 91 | + console.log("this doesn't have a 🍪 object"); |
| 92 | +} |
| 93 | + |
| 94 | +// It will print the window object if run in browser and global if run in node. |
| 95 | +``` |
| 96 | + |
| 97 | +**We can prevent `this` from binding to the global if we use `strict` mode.** |
| 98 | + |
| 99 | +> `this` is `undefined` in a normal function when using the `strict` mode. |
| 100 | +
|
| 101 | +```js |
| 102 | +"use strict"; |
| 103 | + |
| 104 | +function eatCookie() { |
| 105 | + console.log(this); |
| 106 | + console.log("this doesn't have anything!"); |
| 107 | +} |
| 108 | + |
| 109 | +// the above code will not log the global or window object, instead it will print 'undefined'. |
| 110 | +``` |
| 111 | + |
| 112 | +`this` again behaves different if we invoke a function using the `new` operator. |
| 113 | + |
| 114 | +```js |
| 115 | +function Cookie() { |
| 116 | + this.flavour = "I am a chocolate 🍪"; |
| 117 | + console.log(this.flavour, "and I was eaten!"); |
| 118 | +} |
| 119 | + |
| 120 | +const cookie = new Cookie(); |
| 121 | +``` |
| 122 | + |
| 123 | +When we use the `new` operator, it creates a new empty object `{ }`. Then names that object `Cookie` and adds `flavour` property to it. |
| 124 | + |
7 | 125 | --- |
8 | 126 |
|
9 | 127 | See also |
|
0 commit comments