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

Keeping this When You Pass a Method

Keeping this When You Pass a Method

Early Lanes has two lanes. Python’s LaneSession books each pass while a lane is free; once both are taken, it calls on_full, if there is one, with the swimmer’s name. Give it a waitlist’s method, and see what JavaScript does to the keyword this.

In JavaScript, the constructor takes the callback in an options object, { onFull = () => {} } = {}, with a do-nothing arrow as its default, so book() calls this.onFull(pass.swimmer.name) without a check, and booked = []; becomes a field.

The callback is a waitlist’s add(), which puts a name on the list and prints the place. Python passes on_full=waitlist.add, a bound method that carries its waitlist along, and prints two check-ins, then Grace’s place on the waitlist.

The waitlist ports like any class, with names = []; as a field. A line-by-line port of the run passes the same method in the options object:

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 Your next port is the session for Early Lanes, which books each pass while one of its two lanes is free. Once both are taken, it calls on_full, if there is one, with the swimmer’s name.

00:12 Give it a waitlist’s method, and see what JavaScript does to the keyword this.

00:18 In JavaScript, the constructor takes the callback in an options object, with a do-nothing arrow as its default. The booked list becomes a field, since every session starts empty.

00:32 So book() calls the callback without a check, and the rest ports line by line.

00:39 The callback comes from a waitlist. Its add() method puts a name on the list and prints the swimmer’s place. The module passes the waitlist’s add() to the session and books Priya, Tom and Grace.

00:53 Python prints two check-ins, then Grace’s place on the waitlist. What it passed is a bound method: it carries its waitlist along.

01:04 The waitlist ports like the classes before it: names becomes a field, and the message a template literal. The run passes the same method, the waitlist’s add(), in the options object, and books the same three passes.

01:20 Run the port, and Priya and Tom check in, as in Python. What does Grace’s booking do? Your turn: pause the video and predict.

01:36 JavaScript stops with a TypeError. It can’t read push from undefined, so Grace never reaches the waitlist.

01:46 The stack names the function that failed: add(), running as onFull on a LaneSession. In JavaScript, this isn’t fixed when a method is written or looked up.

01:58 Each call sets it, from the object before the dot. Calling early.book() makes this the session inside book(). So this.onFull has the session before the dot, and inside add(), this is the session too.

02:15 A session has no names, so this.names is undefined, and push fails.

02:23 Store the method alone and call it with nothing before the dot. Class code always runs in strict mode, where a call like that gets no object, so this is undefined, and the error reads names.

02:37 A string’s toUpperCase(), stored alone, forgets its string the same way. That’s where the analogy with Python stops. Looking up the waitlist’s add() binds self right then, so Python’s version puts Grace on the waitlist.

02:54 So pass the call, not the method. Give onFull an arrow that calls the waitlist’s add() with the name. The session calls the arrow, which calls add() on the waitlist, so this is the waitlist again.

03:09 In Python, the matching lambda would be needless; in JavaScript, it’s the idiom. Run it, and Grace joins the Early Lanes waitlist, as in Python.

03:22 To book the morning’s list, Python’s book_all() loops with for. A for...of loop would port it, but JavaScript often hands items to a callback, as .forEach() does.

03:33 An arrow has no this of its own, so, like a Python lambda seeing self, it uses bookAll()’s this: the session.

03:43 The same three lines print. Watch out: this isn’t a variable like self. A callback written with function gets its own, and .forEach()’s plain call leaves it undefined.

03:58 In JavaScript, this belongs to the call, not to the method. Pass a method inside an arrow that makes the call, and write callbacks inside methods as arrows.

04:10 A class field holding an arrow keeps its instance wherever it’s passed, since the arrow takes this from the instance it’s made in. The .bind() method and the full rules for this come later.

04:21 Next up, Save Cards and Offer Canceled Lanes: save Priya’s card and read it back, then offer a canceled lane.

Become a Member to join the conversation.