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

Making Functions in a Loop

Making Functions in a Loop

When the route setters close a wall for resetting, the front desk wants a message ready for it. The Python script builds one per wall in a dict comprehension: each wall maps to a small lambda that returns its message, and each lambda also takes wall=wall, a default argument. In this lesson, you ported these messages to JavaScript and saw what becomes of that default:

Language: Python
WALLS = ["Slab", "Cave", "Arete"]

resets = {
    wall: (lambda wall=wall: f"{wall} closed for resetting")
    for wall in WALLS
}

print(resets["Cave"]())

Called for the Cave, Python prints Cave closed for resetting.

That default is there because Python’s closures see a loop variable’s last value. Take it out, and the Cave message says Arete closed for resetting. All three lambdas share one wall, and they read it only when they’re called. By then the loop has finished on Arete. A default is worked out once, when each lambda is made, so wall=wall gives each lambda its own wall.

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 When the route setters close a wall for resetting, the front desk wants a message ready for it. The Python script builds one per wall in a dict comprehension: each wall maps to a small lambda that returns its message.

00:14 Each lambda also takes wall=wall, a default argument. In this lesson, you’ll port these messages to JavaScript and see what becomes of that default.

00:26 Call the Cave message, and Python prints it.

00:31 That default is there because Python’s closures see a loop variable’s last value. Take it out, and the Cave message says Arete. All three lambdas share one wall, and they read it only when they’re called.

00:47 By then the loop has finished on Arete. A default is worked out once, when each lambda is made, so wall=wall gives each its own wall.

00:59 In JavaScript, the walls become a const array. There’s no dict comprehension, so the port starts with an empty object, and a for...of loop adds one property per wall.

01:12 Each lambda becomes an arrow, and the f-string a template literal. The default ports as it is, and the call at the bottom uses dot access.

01:24 Python’s version prints Cave closed for resetting. What does the JavaScript port log? Your turn: pause the video and predict.

01:39 It stops with a ReferenceError: it can’t access wall before initialization. The loop ran without complaint. The error comes from the call, inside the arrow, at its default.

01:54 In a parameter list, the parameters have their own scope. Inside (wall = wall), the name wall already means the parameter, not the loop’s variable.

02:05 The default runs at the call, and reads the parameter before it has a value. That’s the temporal dead zone, as when priceFor() read PRICES too early.

02:16 Python’s wall=wall reads the outer wall, once, when the lambda is made. JavaScript’s default reads later, and from inside.

02:28 So drop the trick: the arrow takes no parameter, and wall means the loop’s variable again. In Python, that version said Arete. What does the JavaScript port log now?

02:41 Your turn: pause the video and predict.

02:49 It logs Cave closed for resetting. A const or let in a loop header makes a new variable for each pass. Slab, Cave and Arete each get their own wall, and each arrow keeps the one from its pass.

03:05 A default couldn’t do that under any name: it runs at every call. The loop header does the binding.

03:14 Watch out for older code that writes the loop header with var: a var belongs to the whole function or module, so all three arrows share one wall variable.

03:24 Cave’s message then says Arete, the late binding Python showed without its default. That’s why older code wraps loop bodies in functions: each call gets its own parameter, as each call of a Python factory function does.

03:40 An array method such as .map() does the same, calling its arrow once per item.

03:47 Python binds each pass by hand, with a default. JavaScript’s loop does the binding itself: write the arrow plainly, and keep const in the loop header.

03:59 Next up is the exercise Number the Wristbands for Each Session. A teammate has ported the wristband counters and the wall messages. Your job is to make the port print what the Python script prints.

Become a Member to join the conversation.