|
| 1 | +# Object methods, "this" |
| 2 | + |
| 3 | +Objects are usually created to represent entities of the real world, like users, orders and so on: |
| 4 | + |
| 5 | +``` |
| 6 | +let user = { |
| 7 | + name: "John", |
| 8 | + age: 30 |
| 9 | +}; |
| 10 | +``` |
| 11 | + |
| 12 | +And, in the real world, a user can act: select something from the shopping cart, login, logout etc. |
| 13 | + |
| 14 | +Actions are represented in JavaScript by functions in properties. |
| 15 | + |
| 16 | +## Method examples |
| 17 | + |
| 18 | +For the start, let’s teach the `user` to say hello: |
| 19 | + |
| 20 | +``` |
| 21 | +let user = { |
| 22 | + name: "John", |
| 23 | + age: 30 |
| 24 | +}; |
| 25 | +
|
| 26 | +user.sayHi = function() { |
| 27 | + alert("Hello!"); |
| 28 | +}; |
| 29 | +
|
| 30 | +user.sayHi(); // Hello! |
| 31 | +``` |
| 32 | + |
| 33 | +Here we’ve just used a Function Expression to create the function and assign it to the property `user.sayHi` of the object. |
| 34 | + |
| 35 | +Then we can call it. The user can now speak! |
| 36 | + |
| 37 | +A function that is the property of an object is called its method. |
| 38 | + |
| 39 | +So, here we’ve got a method `sayHi` of the object `user`. |
| 40 | + |
| 41 | +Of course, we could use a pre-declared function as a method, like this: |
| 42 | + |
| 43 | +``` |
| 44 | +let user = { |
| 45 | + // ... |
| 46 | +}; |
| 47 | +
|
| 48 | +// first, declare |
| 49 | +function sayHi() { |
| 50 | + alert("Hello!"); |
| 51 | +}; |
| 52 | +
|
| 53 | +// then add as a method |
| 54 | +user.sayHi = sayHi; |
| 55 | +
|
| 56 | +user.sayHi(); // Hello! |
| 57 | +``` |
| 58 | + |
| 59 | +*** |
| 60 | + |
| 61 | +#### Object-oriented programming |
| 62 | + |
| 63 | +When we write our code using objects to represent entities, that’s called an [object-oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming "object-oriented programming"), in short: “OOP”. |
| 64 | + |
| 65 | +OOP is a big thing, an interesting science of its own. How to choose the right entities? How to organize the interaction between them? That’s architecture, and there are great books on that topic, like “Design Patterns: Elements of Reusable Object-Oriented Software” by E.Gamma, R.Helm, R.Johnson, J.Vissides or “Object-Oriented Analysis and Design with Applications” by G.Booch, and more. We’ll scratch the surface of that topic later in the chapter Objects, classes, inheritance. |
| 66 | + |
| 67 | +*** |
| 68 | + |
| 69 | +### Method shorthand |
| 70 | + |
| 71 | +There exists a shorter syntax for methods in an object literal: |
| 72 | + |
| 73 | +``` |
| 74 | +// these objects do the same |
| 75 | +
|
| 76 | +let user = { |
| 77 | + sayHi: function() { |
| 78 | + alert("Hello"); |
| 79 | + } |
| 80 | +}; |
| 81 | +
|
| 82 | +// method shorthand looks better, right? |
| 83 | +let user = { |
| 84 | + sayHi() { // same as "sayHi: function()" |
| 85 | + alert("Hello"); |
| 86 | + } |
| 87 | +}; |
| 88 | +``` |
| 89 | + |
| 90 | +As demonstrated, we can omit `"function"` and just write `sayHi()`. |
| 91 | + |
| 92 | +To tell the truth, the notations are not fully identical. There are subtle differences related to object inheritance (to be covered later), but for now they do not matter. In almost all cases the shorter syntax is preferred. |
| 93 | + |
| 94 | +## “this” in methods |
| 95 | + |
| 96 | +It’s common that an object method needs to access the information stored in the object to do its job. |
| 97 | + |
| 98 | +For instance, the code inside `user.sayHi()` may need the name of the `user`. |
| 99 | + |
| 100 | +***To access the object, a method can use the this keyword.*** |
| 101 | + |
| 102 | +The value of `this` is the object “before dot”, the one used to call the method. |
| 103 | + |
| 104 | +For instance: |
| 105 | + |
| 106 | +``` |
| 107 | +let user = { |
| 108 | + name: "John", |
| 109 | + age: 30, |
| 110 | +
|
| 111 | + sayHi() { |
| 112 | + alert(this.name); |
| 113 | + } |
| 114 | +
|
| 115 | +}; |
| 116 | +
|
| 117 | +user.sayHi(); // John |
| 118 | +``` |
| 119 | + |
| 120 | +Here during the execution of `user.sayHi()`, the value of `this` will be `user`. |
| 121 | + |
| 122 | +Technically, it’s also possible to access the object without `this`, by referencing it via the outer variable: |
| 123 | + |
| 124 | +``` |
| 125 | +let user = { |
| 126 | + name: "John", |
| 127 | + age: 30, |
| 128 | +
|
| 129 | + sayHi() { |
| 130 | + alert(user.name); // "user" instead of "this" |
| 131 | + } |
| 132 | +
|
| 133 | +}; |
| 134 | +``` |
| 135 | + |
| 136 | +... But such code is unreliable. If we decide to copy `user` to another variable, e.g. `admin = user` and overwrite `user` with something else, then it will access the wrong object. |
| 137 | + |
| 138 | +That’s demonstrated below: |
| 139 | + |
| 140 | +``` |
| 141 | +let user = { |
| 142 | + name: "John", |
| 143 | + age: 30, |
| 144 | +
|
| 145 | + sayHi() { |
| 146 | + alert( user.name ); // leads to an error |
| 147 | + } |
| 148 | +
|
| 149 | +}; |
| 150 | +
|
| 151 | +
|
| 152 | +let admin = user; |
| 153 | +user = null; // overwrite to make things obvious |
| 154 | +
|
| 155 | +admin.sayHi(); // Whoops! inside sayHi(), the old name is used! error! |
| 156 | +``` |
| 157 | + |
| 158 | +If we used `this.name` instead of `user.name` inside the `alert`, then the code would work. |
| 159 | + |
| 160 | +## “this” is not bound |
| 161 | + |
| 162 | +In JavaScript, “this” keyword behaves unlike most other programming languages. First, it can be used in any function. |
| 163 | + |
| 164 | +There’s no syntax error in the code like that: |
| 165 | + |
| 166 | +``` |
| 167 | +function sayHi() { |
| 168 | + alert( this.name ); |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +The value of `this` is evaluated during the run-time. And it can be anything. |
| 173 | + |
| 174 | +For instance, the same function may have different “this” when called from different objects: |
| 175 | + |
| 176 | +``` |
| 177 | +let user = { name: "John" }; |
| 178 | +let admin = { name: "Admin" }; |
| 179 | +
|
| 180 | +function sayHi() { |
| 181 | + alert( this.name ); |
| 182 | +} |
| 183 | +
|
| 184 | +// use the same functions in two objects |
| 185 | +user.f = sayHi; |
| 186 | +admin.f = sayHi; |
| 187 | +
|
| 188 | +// these calls have different this |
| 189 | +// "this" inside the function is the object "before the dot" |
| 190 | +user.f(); // John (this == user) |
| 191 | +admin.f(); // Admin (this == admin) |
| 192 | +
|
| 193 | +admin['f'](); // Admin (dot or square brackets access the method – doesn't matter) |
| 194 | +``` |
| 195 | + |
| 196 | +Actually, we can call the function without an object at all: |
| 197 | + |
| 198 | +``` |
| 199 | +function sayHi() { |
| 200 | + alert(this); |
| 201 | +} |
| 202 | +
|
| 203 | +sayHi(); // undefined |
| 204 | +``` |
| 205 | + |
| 206 | +In this case `this` is `undefined` in strict mode. If we try to access `this.name`, there will be an error. |
| 207 | + |
| 208 | +In non-strict mode (if one forgets `use strict`) the value of `this` in such case will be the global object (`window` in a browser, we’ll get to it later). This is a historical behavior that `"use strict"` fixes. |
| 209 | + |
| 210 | +Please note that usually a call of a function that uses `this` without an object is not normal, but rather a programming mistake. If a function has `this`, then it is usually meant to be called in the context of an object. |
| 211 | + |
| 212 | +*** |
| 213 | + |
| 214 | +#### The consequences of unbound `this` |
| 215 | + |
| 216 | +If you come from another programming language, then you are probably used to the idea of a "bound `this`", where methods defined in an object always have `this` referencing that object. |
| 217 | + |
| 218 | +In JavaScript `this` is “free”, its value is evaluated at call-time and does not depend on where the method was declared, but rather on what’s the object “before the dot”. |
| 219 | + |
| 220 | +The concept of run-time evaluated `this` has both pluses and minuses. On the one hand, a function can be reused for different objects. On the other hand, greater flexibility opens a place for mistakes. |
| 221 | + |
| 222 | +Here our position is not to judge whether this language design decision is good or bad. We’ll understand how to work with it, how to get benefits and evade problems. |
| 223 | + |
| 224 | +*** |
| 225 | + |
| 226 | +## Internals: Reference Type |
| 227 | + |
| 228 | +*** |
| 229 | + |
| 230 | +#### In-depth language feature |
| 231 | + |
| 232 | +This section covers an advanced topic, to understand certain edge-cases better. |
| 233 | + |
| 234 | +If you want to go on faster, it can be skipped or postponed. |
| 235 | + |
| 236 | +*** |
| 237 | + |
| 238 | +An intricate method call can lose `this`, for instance: |
| 239 | + |
| 240 | +``` |
| 241 | +let user = { |
| 242 | + name: "John", |
| 243 | + hi() { alert(this.name); }, |
| 244 | + bye() { alert("Bye"); } |
| 245 | +}; |
| 246 | +
|
| 247 | +user.hi(); // John (the simple call works) |
| 248 | +
|
| 249 | +// now let's call user.hi or user.bye depending on the name |
| 250 | +(user.name == "John" ? user.hi : user.bye)(); // Error! |
| 251 | +``` |
| 252 | + |
| 253 | +On the last line there is a ternary operator that chooses either `user.hi` or `user.bye`. In this case the result is `user.hi`. |
| 254 | + |
| 255 | +The method is immediately called with parentheses `()`. But it doesn’t work right! |
| 256 | + |
| 257 | +You can see that the call results in an error, cause the value of `"this"` inside the call becomes `undefined`. |
| 258 | + |
| 259 | +This works (object dot method): |
| 260 | + |
| 261 | +``` |
| 262 | +user.hi(); |
| 263 | +``` |
| 264 | + |
| 265 | +This doesn’t (evaluated method): |
| 266 | + |
| 267 | +``` |
| 268 | +(user.name == "John" ? user.hi : user.bye)(); // Error! |
| 269 | +``` |
| 270 | + |
| 271 | +Why? If we want to understand why it happens, let’s get under the hood of how `obj.method()` call works. |
| 272 | + |
| 273 | +Looking closely, we may notice two operations in `obj.method()` statement: |
| 274 | + |
| 275 | +1. First, the dot `'.'` retrieves the property `obj.method`. |
| 276 | + |
| 277 | +2. Then parentheses `()` execute it. |
| 278 | + |
| 279 | +So, how does the information about `this` gets passed from the first part to the second one? |
| 280 | + |
| 281 | +If we put these operations on separate lines, then `this` will be lost for sure: |
| 282 | + |
| 283 | +``` |
| 284 | +let user = { |
| 285 | + name: "John", |
| 286 | + hi() { alert(this.name); } |
| 287 | +} |
| 288 | +
|
| 289 | +// split getting and calling the method in two lines |
| 290 | +let hi = user.hi; |
| 291 | +hi(); // Error, because this is undefined |
| 292 | +``` |
| 293 | + |
| 294 | +Here `hi = user.hi` puts the function into the variable, and then on the last line it is completely standalone, and so there’s no `this`. |
| 295 | + |
| 296 | +***To `make user.hi()` calls work, JavaScript uses a trick – the dot '.' returns not a function, but a value of the special [Reference Type](https://tc39.github.io/ecma262/#sec-reference-specification-type "Reference Type").*** |
| 297 | + |
| 298 | +The Reference Type is a “specification type”. We can’t explicitly use it, but it is used internally by the language. |
| 299 | + |
| 300 | +The value of Reference Type is a three-value combination `(base, name, strict)`, where: |
| 301 | + |
| 302 | +* `base` is the object. |
| 303 | + |
| 304 | +* `name` is the property. |
| 305 | + |
| 306 | +* `strict` is true if `use strict` is in effect. |
| 307 | + |
| 308 | +The result of a property access `user.hi` is not a function, but a value of Reference Type. For `user.hi` in strict mode it is: |
| 309 | + |
| 310 | +``` |
| 311 | +// Reference Type value |
| 312 | +(user, "hi", true) |
| 313 | +``` |
| 314 | + |
| 315 | +When parentheses `()` are called on the Reference Type, they receive the full information about the object and it’s method, and can set the right `this` (`=user` in this case). |
| 316 | + |
| 317 | +Any other operation like assignment `hi = user.hi` discards the reference type as a whole, takes the value of `user.hi` (a function) and passes it on. So any further operation “loses” `this`. |
| 318 | + |
| 319 | +So, as the result, the value of `this` is only passed the right way if the function is called directly using a dot `obj.method()` or square brackets `obj[method]()` syntax (they do the same here). |
| 320 | + |
| 321 | +## Arrow functions have no “this” |
| 322 | + |
| 323 | +Arrow functions are special: they don’t have their “own” `this`. If we reference `this` from such a function, it’s taken from the outer “normal” function. |
| 324 | + |
| 325 | +For instance, here `arrow()` uses `this` from the outer `user.sayHi()` method: |
| 326 | + |
| 327 | +``` |
| 328 | +let user = { |
| 329 | + firstName: "Ilya", |
| 330 | + sayHi() { |
| 331 | + let arrow = () => alert(this.firstName); |
| 332 | + arrow(); |
| 333 | + } |
| 334 | +}; |
| 335 | +
|
| 336 | +user.sayHi(); // Ilya |
| 337 | +``` |
| 338 | + |
| 339 | +That’s a special feature of arrow functions, it’s useful when we actually do not want to have a separate this, but rather to take it from the outer context. Later in the chapter Arrow functions revisited we’ll go more deeply into arrow functions. |
| 340 | + |
| 341 | +## Summary |
| 342 | + |
| 343 | +* Functions that are stored in object properties are called “methods”. |
| 344 | + |
| 345 | +* Methods allow objects to “act” like `object.doSomething()`. |
| 346 | + |
| 347 | +* Methods can reference the object as `this`. |
| 348 | + |
| 349 | +The value of `this` is defined at run-time. |
| 350 | + |
| 351 | +* When a function is declared, it may use `this`, but that `this` has no value until the function is called. |
| 352 | + |
| 353 | +* That function can be copied between objects. |
| 354 | + |
| 355 | +* When a function is called in the “method” syntax: `object.method()`, the value of `this` during the call is `object`. |
| 356 | + |
| 357 | +Please note that arrow functions are special: they have no `this`. When `this` is accessed inside an arrow function, it is taken from outside. |
0 commit comments