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

Read this From the Call

In this video, Mei writes one reminder() function for her plants in a new module in her plant-tracker folder, and calls it four different ways. Each time, the form of the call, or a binding made with .bind(), decides what this is inside the function.

Key Points

  • A method call (goldie.reminder("Monday")): this is the object before the dot, goldie.
  • A plain call (reminder("Friday")): there’s no dot, so there’s no object. Your file is a module, so it’s strict, and this is undefined. Reading this.name then throws a TypeError, and the script stops.
  • .call() and .apply(): functions are values, and every function has built-in methods. reminder.call(spike, "Sunday") makes spike the this for that one call and passes the rest of the arguments as usual. .apply() does the same job but takes the function’s arguments in an array: reminder.apply(spike, ["Sunday"]). Spike’s record stays plain data and never needs a reminder property.
  • A bound function: reminder.bind(spike) doesn’t call reminder(). It returns a new function whose this is fixed to spike. It always calls reminder() with this set to spike, so even with no dot, as in spikeReminder("Saturday"), this is still spike.
  • The rule: to find this, first check whether the function is bound. If it is, this is the object it was bound to. If not, look at the call. Where a function is written decides which variables it can see. But for a function written with the function keyword, like reminder(), or a shorthand method, like needsWater(), this comes from the call, or from .bind(). Arrow functions are the exception. They work differently, and you’ll see how later in this course.
The call this is
spikeReminder(), from reminder.bind(spike) (check first) spike, the object it was bound to
goldie.reminder() goldie, the object before the dot
reminder() undefined (strict code)
reminder.call(spike), reminder.apply(spike) spike, the first argument

The Final Code

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 You know that this in a method is the object before the dot. You’ve also seen that a plain call in strict code gets this set to undefined.

00:09 In this video, you’ll read this from how a function is called, in four kinds of call. Mei wants watering reminders for her plants. In her plant-tracker folder, she starts a new module, reminders.js.

00:23 She writes reminder(), one function for every plant. It takes a day, and it uses this to read the plant’s name. Goldie gets the function as a property, so Goldie has a reminder method.

00:35 Spike the cactus is a plain record, with a name and nothing else. The function never says which plant it’s for. So where does this come from?

00:46 Start with the call you know. Line 8 calls goldie.reminder() with Monday. There’s an object before the dot, so this is Goldie. The terminal shows Water Goldie on Monday.

00:59 Line 9 calls reminder by its own name, with Friday. There’s no dot, so there’s no object to hand over. Your file is a module, so it’s strict, and this gets undefined, not an object.

01:13 Reading a name from undefined throws a TypeError, and the script stops. It’s the same function in both calls, and it didn’t change. Only the call did.

01:25 Mei wants Spike’s reminder too. But Spike’s record is plain data, and she’d rather not add a method to it. So she swaps line 9 for reminder.call(), with Spike and Sunday.

01:37 The terminal shows Water Spike on Sunday.

01:41 Functions are values, and every function has some methods built in. One of them, call, sets this for a single call. Its first argument becomes this, and the rest go to the function as usual.

01:54 The call hands Spike over as this, for that one call only. Line 10 uses apply, its twin. It does the same job, but it takes the function’s arguments in an array.

02:06 So it prints the same line.

02:10 Writing call every time gets long. Mei would rather have a reminder that always means Spike. For that, line 12 uses bind, another method every function has.

02:22 It doesn’t call reminder. It returns a new function whose this is fixed to Spike, and Mei keeps it in a variable, spikeReminder, on the same line.

02:32 Now look at line 13. It calls spikeReminder with Saturday, and there’s no dot in front of it. Your turn: pause the video and predict what line 13 prints.

02:48 It prints Water Spike on Saturday. A bound function always calls reminder with this set to Spike. So even with no dot, it’s Spike.

03:00 So to find this, first ask: is it a bound function? If it is, this is the object it was bound to. If not, look at the call. With an object before the dot, this is that object.

03:13 With no dot at all, it’s undefined in strict code. With call or apply, it’s the first argument.

03:21 Where a function is written decides which variables it can see. But for a function written with the function keyword, like reminder, or a shorthand method, like needsWater, this comes from the call, or from bind.

03:34 Arrow functions are the exception. They work differently, and you’ll see how later in this course. That’s all you need about them for now.

03:42 Mei now wants a shelf that waters every plant in one go. Next, in Keep this in Callbacks, you’ll build that shelf and read this from the calls it makes.

Become a Member to join the conversation.