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

Each wall needs a ready message. The Python script builds the messages in a dict comprehension, with wall=wall defaults: lambda wall=wall: f"{wall} closed for resetting". For the Cave, it prints Cave closed for resetting. Without the default, it says Arete closed for resetting: Python’s late binding, where closures see a loop variable’s last value.

  • The default throws in JavaScript. Ported into a for...of loop that fills an empty object, with each lambda as an arrow and the default as it was, resets[wall] = (wall = wall) => ... makes resets.Cave() stop with ReferenceError: Cannot access 'wall' before initialization. The loop ran without complaint: the error comes from the call, inside the arrow, at its default.
  • A default reads at the call, from inside. In a parameter list, wall already means the parameter, not the loop’s variable. The default runs at the call and reads it before it has a value: the temporal dead zone, as when priceFor() read PRICES too early (in “Knowing When a Name Exists”, line 8 called priceFor() before line 10’s const PRICES ran). Python’s wall=wall reads the outer wall once, when the lambda is made.
  • The loop header does the binding. Drop the trick and wall means the loop’s variable again. Where Python said Arete, JavaScript logs the Cave’s own message: a const or let in a loop header makes a new variable for each pass, where Python’s loop rebinds one, so each arrow keeps the wall from its pass.
  • Watch out for var. A var in the loop header belongs to the whole function or module, so all three arrows share one wall, and Cave’s message says Arete, the late binding Python showed without its default. That’s why older code wraps loop bodies in functions, where each call gets its own parameter, as in a Python factory. An array method such as .map() does the same, calling its arrow once per item.
Language: JavaScript
const WALLS = ["Slab", "Cave", "Arete"];

const resets = {};
for (const wall of WALLS) {
  resets[wall] = () => `${wall} closed for resetting`;
}

console.log(resets.Cave());

The Cave’s arrow logs 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 a wall closes for resetting, the desk wants a ready message for it, and you’ll see how Python’s default argument trick fares in JavaScript. The Python script builds the messages in a dict comprehension, with wall=wall defaults.

00:14 For the Cave, it prints the Cave’s message. Without the default, it says Arete. That’s Python’s late binding: its closures see a loop variable’s last value.

00:29 With the default back, the port fills an empty object in a for...of loop, since JavaScript has no dict comprehension. Each lambda becomes an arrow, and the default comes along unchanged.

00:42 What does the JavaScript port log for the Cave? Your turn: pause the video and predict.

00:53 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:07 In a parameter list, wall already means the parameter, not the loop’s variable. The default runs at the call and reads it before it has a value: the temporal dead zone, as when priceFor() read PRICES too early.

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

01:32 So drop the trick, and wall means the loop’s variable again. In Python, that version said Arete, but JavaScript logs Cave closed for resetting.

01:43 A const or let in a loop header makes a new variable for each pass, where Python’s loop rebinds one. Each arrow keeps the wall from its pass.

01:53 The header does the binding: a default only reads wall at the call.

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

02:09 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 in a Python factory.

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

02:29 Python binds each pass by hand. JavaScript’s loop does it itself: write the arrow plainly, and keep const in the loop header. Next up is the exercise Number the Wristbands for Each Session.

02:42 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.