|
| 1 | +--- |
| 2 | +chapter : js101 |
| 3 | +section: 10 |
| 4 | +title: The "this" Keyword |
| 5 | +attribution: |
| 6 | +--- |
| 7 | + |
| 8 | +In JavaScript, as in most object-oriented programming languages, `this` is a |
| 9 | +special keyword that is used within methods to refer to the object on which a |
| 10 | +method is being invoked. The value of `this` is determined using a simple series |
| 11 | +of steps: |
| 12 | + |
| 13 | +- If the function is invoked using `Function.call` or `Function.apply`, this will |
| 14 | + be set to the first argument passed to `call`/`apply`. If the first argument |
| 15 | + passed to `call`/`apply` is null or undefined, `this` will refer to the global |
| 16 | + object (which is the `window` object in Web browsers). |
| 17 | +- If the function being invoked was created using `Function.bind`, `this` will be |
| 18 | + the first argument that was passed to `bind` at the time the function was |
| 19 | + created. |
| 20 | +- If the function is being invoked as a method of an object, `this` will refer to |
| 21 | + that object. |
| 22 | +- Otherwise, the function is being invoked as a standalone function not |
| 23 | + attached to any object, and `this` will refer to the global object. |
| 24 | + |
| 25 | +<div class="example" markdown="1"> |
| 26 | +A function invoked using Function.call |
| 27 | + |
| 28 | + var myObject = { |
| 29 | + sayHello : function() { |
| 30 | + console.log('Hi! My name is ' + this.myName); |
| 31 | + }, |
| 32 | + |
| 33 | + myName : 'Rebecca' |
| 34 | + }; |
| 35 | + |
| 36 | + var secondObject = { |
| 37 | + myName : 'Colin' |
| 38 | + }; |
| 39 | + |
| 40 | + myObject.sayHello(); // logs 'Hi! My name is Rebecca' |
| 41 | + myObject.sayHello.call(secondObject); // logs 'Hi! My name is Colin' |
| 42 | +</div> |
| 43 | + |
| 44 | +<div class="example" markdown="1"> |
| 45 | +A function created using Function.bind |
| 46 | + |
| 47 | + var myName = 'the global object', |
| 48 | + |
| 49 | + sayHello = function () { |
| 50 | + console.log('Hi! My name is ' + this.myName); |
| 51 | + }, |
| 52 | + |
| 53 | + myObject = { |
| 54 | + myName : 'Rebecca' |
| 55 | + }; |
| 56 | + |
| 57 | + var myObjectHello = sayHello.bind(myObject); |
| 58 | + |
| 59 | + sayHello(); // logs 'Hi! My name is the global object' |
| 60 | + myObjectHello(); // logs 'Hi! My name is Rebecca' |
| 61 | +</div> |
| 62 | + |
| 63 | +<div class="example" markdown="1"> |
| 64 | +A function being attached to an object at runtime |
| 65 | + |
| 66 | + var myName = 'the global object', |
| 67 | + |
| 68 | + sayHello = function() { |
| 69 | + console.log('Hi! My name is ' + this.myName); |
| 70 | + }, |
| 71 | + |
| 72 | + myObject = { |
| 73 | + myName : 'Rebecca' |
| 74 | + }, |
| 75 | + |
| 76 | + secondObject = { |
| 77 | + myName : 'Colin' |
| 78 | + }; |
| 79 | + |
| 80 | + myObject.sayHello = sayHello; |
| 81 | + secondObject.sayHello = sayHello; |
| 82 | + |
| 83 | + sayHello(); // logs 'Hi! My name is the global object' |
| 84 | + myObject.sayHello(); // logs 'Hi! My name is Rebecca' |
| 85 | + secondObject.sayHello(); // logs 'Hi! My name is Colin' |
| 86 | +</div> |
| 87 | + |
| 88 | +<div class="note" markdown="1'> |
| 89 | +## Note |
| 90 | + |
| 91 | +When invoking a function deep within a long namespace, it is often tempting to |
| 92 | +reduce the amount of code you need to type by storing a reference to the actual |
| 93 | +function as a single, shorter variable. It is important not to do this with |
| 94 | +instance methods as this will cause the value of `this` within the function to |
| 95 | +change, leading to incorrect code operation. For instance: |
| 96 | + |
| 97 | + var myNamespace = { |
| 98 | + myObject : { |
| 99 | + sayHello : function() { |
| 100 | + console.log('Hi! My name is ' + this.myName); |
| 101 | + }, |
| 102 | + |
| 103 | + myName : 'Rebecca' |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + var hello = myNamespace.myObject.sayHello; |
| 108 | + |
| 109 | + hello(); // logs 'Hi! My name is undefined' |
| 110 | + |
| 111 | +You can, however, safely reduce everything up to the object on which the method is invoked: |
| 112 | + |
| 113 | + var myNamespace = { |
| 114 | + myObject : { |
| 115 | + sayHello : function() { |
| 116 | + console.log('Hi! My name is ' + this.myName); |
| 117 | + }, |
| 118 | + |
| 119 | + myName : 'Rebecca' |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + var obj = myNamespace.myObject; |
| 124 | + |
| 125 | + obj.sayHello(); // logs 'Hi! My name is Rebecca' |
| 126 | +</div> |
0 commit comments