Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Choose Arrows or Methods for this

In this video, you pick up Mei’s shelf where its waterAll() method lost the shelf inside a .forEach() callback. An arrow function fixes it, because an arrow has no this of its own. Then you see why that same fact makes an arrow the wrong choice for a method.

Key Points

  • The puzzle: shelf.waterAll() has the shelf before the dot, so inside waterAll(), this is the shelf. But .forEach() calls a function (plant) { ... } callback plainly, so that function gets its own this from a plain call: undefined in strict code. this.waterPlant(plant) then throws a TypeError.
  • An arrow looks outward: an arrow function doesn’t get a this of its own, so no call can fill one in. When it reads this, it looks outward, to the code it’s written in, the way it finds any variable it doesn’t declare (though this isn’t a variable you can declare or set).
  • The nearest regular function: the lookup stops at the nearest regular function around the arrow, because every regular function has a this of its own. Here, that’s waterAll(), and its this is the shelf, so this.waterPlant(plant) is a method call with the shelf before the dot. This is called lexical this.
  • An arrow’s this comes from the code it’s written in: here, the this of waterAll(). A function gets its own this from how it’s called.
  • An arrow as a method: writing waterPlant: (plant) => { ... } in the shelf’s object literal makes this inside it undefined, even when it’s called as this.waterPlant(plant) with the shelf before the dot. The arrow takes no this from the call. It looks outward instead, and the braces around the shelf are an object literal, not a function, so they hold no this.
  • The module’s top level: the lookup reaches the module’s top level, where you’re outside every function and this is undefined. This arrow gets undefined because it was written inside the shelf’s braces but still at the module’s top level, whatever object is before the dot when it’s called. The script stops with TypeError: Cannot read properties of undefined (reading 'wateredToday').
  • The fix: write the method with the method shorthand again, waterPlant(plant) { ... }. That’s why you write methods with the shorthand.
Where Write Example
A method the method shorthand waterPlant(plant) { ... }
A callback inside a method an arrow function (plant) => { ... }
A function on its own (no object, so it needs no this) a function declaration that takes its record as an argument function plantNeedsWater(plant) { ... }

The rule: an arrow uses the this of the code it’s written in, so inside a method it gets that method’s this. Any other function takes it from the call (or from .bind()).

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Mei’s shelf got a waterAll() method, and the shelf got lost inside it. In this video, you’ll see why an arrow function fixes that, and where an arrow doesn’t belong.

00:11 Here’s her file again. Line 13 starts the method. On line 14, it hands forEach a function written with the function keyword. Running water.js stops with a TypeError on line 15.

00:25 Read this from each call. Mei calls shelf.waterAll() with the shelf before the dot, so inside waterAll, this is the shelf.

00:35 But forEach calls the inner function plainly, once per plant. That function gets its own this from that call, and a plain call in strict code gives it undefined.

00:45 So the inner function never sees the shelf.

00:51 Now swap the inner function for an arrow function, with the same body. Run water.js again, and both plants are on the list.

01:00 Yet forEach still calls the arrow plainly, so the call isn’t what changed. An arrow function doesn’t get a this of its own, so no call can fill one in.

01:11 When the arrow reads this, it looks outward, to the code it’s written in. It finds this the way it finds any variable it doesn’t declare, though this isn’t a variable you can declare or set.

01:22 The lookup stops at the nearest regular function around the arrow, because every regular function has a this of its own. Here, that’s waterAll, and waterAll’s this is the shelf.

01:34 So line 15 calls waterPlant with the shelf before the dot. This is called lexical this. An arrow’s this comes from the code it’s written in: here, waterAll’s this.

01:48 A function gets its own this from how it’s called.

01:53 Mei likes the arrow, so she writes waterPlant as one too. Line 9 now stores an arrow function as the method, and its body doesn’t change. The waterAll method and its arrow callback stay as they were.

02:06 Your turn: pause the video and predict whether water.js prints both plants this time.

02:16 It doesn’t. The script stops with a TypeError on line 10. Line 10 reads wateredToday from this, and this is undefined there, not the shelf.

02:29 The call on line 15 still has the shelf before the dot. But an arrow takes no this from a call, so that dot has nothing to fill. The arrow looks outward from where it’s written.

02:41 The braces around the shelf are an object literal, not a function, so they hold no this. The lookup goes past them, to the top level of the module.

02:51 At a module’s top level, you’re outside every function, and no call sets this. JavaScript leaves it undefined there. So this arrow, written inside the shelf’s braces but still at the module’s top level, gets undefined, whatever object is before the dot.

03:09 The fix is to write waterPlant with the method shorthand again. Run water.js, and Goldie and Moss are back on the list. That’s why you’ve written methods with the shorthand all along.

03:22 So choose by where this should come from. A method needs the object it’s called on, so write it with the method shorthand. A callback inside a method needs the method’s this, so write it as an arrow.

03:35 A function that stands on its own has no object, so it needs no this: write it as a function declaration that takes its record as an argument.

03:45 An arrow uses the this of the code it’s written in, so inside a method it gets that method’s this. Any other function gets its own this from the call.

03:56 Mei’s shelf now waters every plant from a single call. Next, in Exercise: Fix the Lost this, you’ll get another of Mei’s shelves watering all of her plants.

Become a Member to join the conversation.