Lesson 6 of 198:42 video

The JavaScript this keyword

How a function kept in a property becomes a method, what the word this means inside it (whatever is left of the dot), why a method handed to setTimeout prints undefined, how an arrow function or bind brings the object back, and what it means that arrow functions have no this of their own.

In this lesson

  1. 0:00 Intro
  2. 1:02 Methods
  3. 2:27 Left of the dot
  4. 4:33 Back to the hook
  5. 6:23 Arrow functions and this
  6. 8:06 Next time

Transcript

Intro

Here's a rocket with a launch method. Call it, and it reports its fuel: liftoff with eighty percent fuel. Now ask setTimeout to call the same method one second from now. One second later: liftoff with undefined percent fuel. Same rocket. Same method. Not one character inside it changed. All that changed is who made the call. Where did the fuel go? Today, we find out.

By the end of this video, you'll understand three things. One: how a function kept inside an object becomes a method, and what the word this means inside it. Two: how JavaScript decides what this means, at the call and not before, and how a method loses its object on the way to setTimeout. Three: two ways to put the rocket back, and what arrow functions do differently with this that makes them the right tool for callbacks.

Methods

Last time, we built arrays and objects, and let callbacks loop through them. Along the way we met a word: method, a function kept in a property. We've been calling methods since the first episode: log on console, push and map on arrays. Today we write our own. That takes one new word, this, and that one word is the subject of the whole episode. We'll see what it means inside a method, how it goes missing, which is what happened in the hook, two ways to bring it back, and finally a debt from the functions episode: what it means that an arrow function has no this of its own.

A function is a value, and an object holds values by name. Put the two together: a property can hold a function. Here's the rocket, with a launch property holding a function expression, the second way to write a function from the functions episode. To call it, write the object, a dot, the name, and parentheses: rocket dot launch. Since 2015 there's a shorter way to write it: drop the colon and the word function, and put the parentheses right after the name. Same method, less to type, and it's how you'll see methods written from here on. So far launch only prints a word. Nothing inside it touches the rocket. To report the fuel, the function needs a way to reach the object it lives in.

Left of the dot

The obvious move is to write rocket dot fuel inside the method. Try it: liftoff with eighty percent fuel. It works. But a method is meant to belong to whatever object holds it, and the same function can end up in more than one. Give Heron the same function: heron dot launch equals rocket dot launch. Call heron dot launch, and it says eighty. Kestrel's fuel, because the code says rocket. What we want the method to say is: the fuel of whichever object I was called on. JavaScript has a word for that object: this. Replace rocket with this, and heron dot launch says forty. Kestrel still says eighty. One function, two rockets, two answers. Inside a method, this means the object the method was called on, and you can read that straight off the code: it's whatever is left of the dot. The word works for writing too. A refuel method that adds to this dot fuel tops up whichever rocket it was called on. And every method you've used works the same way: push adds to whichever array is left of its dot.

Here's the rule in full. The word this is filled in at the call, not where the function is written. The function doesn't know which object it belongs to. It finds out at every call, from what's left of the dot. Put the three rockets in an array and run them all: for each ship, ship dot launch. Three calls, three different objects left of the dot, three fuel reports from one function. So take the dot away. Copy the method into a plain variable, launch, and call it with nothing in front. Nothing left of the dot means nothing for this to stand for. In modern JavaScript it stands for undefined, and reading fuel from undefined stops the program: TypeError, cannot read properties of undefined. The function is fine. The call is missing its object. Keep that sentence. It explains every this bug you'll ever meet.

Back to the hook

Which brings us back to the hook. setTimeout, from the functions episode: hand it a function and a number of milliseconds, and it calls the function when the time is up. Look at the line again. There's a dot in it, rocket dot launch. But look at what follows: a comma, not parentheses. That dot isn't a call. It reads the function out of the rocket and hands it over, the way we handed console dot log to the countdown. One second later, setTimeout makes the call, and the rocket is nowhere near it. In a browser, setTimeout calls it with a different object left of the dot: window, the built-in object that stands for the browser tab, where the browser keeps its globals, setTimeout itself among them. window has no fuel, so this dot fuel is undefined. The dot was in the code, but not at the call. And that's how this goes missing in real programs. Nobody writes the plain call from a minute ago. They hand a method to something else to call later, and it arrives alone. A timer today. Later in this series, a button.

Two fixes. One: don't hand over the method. Hand over an arrow function that calls it. Now what setTimeout gets is the arrow function, and inside it, rocket dot launch is a real call, with the rocket left of the dot. Liftoff with eighty percent fuel. Two: bind. Every function has a method called bind. Call rocket dot launch dot bind, with the rocket as the argument, and you get back a new function whose this is fixed to the rocket for good, no matter who calls it or how. Hand that to setTimeout: eighty percent. Wrap the call, or bind the function. Either way, the rocket is back.

Arrow functions and this

When we met arrow functions, we left a note: an arrow function has no this of its own. Time to see what that means. Write launch as an arrow function. Call probe dot launch, with the probe right there, left of the dot. And it fails. An arrow function doesn't look left of the dot. It has no this of its own, so it borrows the this of the code around it, wherever it was written. Ours was written at the top of the file, outside any method, so there's no probe for it to borrow, and the TypeError is back. Written as an arrow function, a method can't see its own object. So a method is never an arrow function.

Now the other side of the same fact, and it's the reason arrow functions took over. Give the rocket a countdown method: wait one second, then launch. Inside it, setTimeout needs a function. Write that function the old way, with the keyword function, and inside it, this is whatever setTimeout hands over: window, which has no launch. TypeError: this dot launch is not a function. Write it as an arrow function instead. An arrow function has no this of its own, so it borrows the this of the code around it. The code around it is the countdown method, where this is the rocket. One second later: liftoff with eighty percent fuel. Inside a method, callbacks are arrow functions. Not because they're shorter. Because they keep this.

Next time

That's the word this in one line: a method is a function kept in a property, this means whatever is left of the dot when it's called, hand the method away and it arrives without its object, and an arrow function borrows the this of the code around it. Next time, we build a whole fleet from one blueprint, with classes, and find out where methods really live.

Learn more