Loading topic
var, let, and const at a basic level, and have passed a function as an argument before. You also need the idea from the previous chapter that functions are values: they can be stored in variables, returned, an6 min readvar, let, and const, write and call functions, and read basic control flow such as if, for, and switch. The previous chapter showed that where code is written decides what it can see. This chapter covers the size of the region each decl5 min readvar, let, and const differ, and you have read the earlier chapters on lexical scope and on block versus function scope. Here we look at the lookup itself: what JavaScript does, step by step, when your code uses a name like `to6 min readlet, const, and var differ in scope, and how the scope chain finds a variable by searching outward. Here you will look at the machinery the JavaScript specification uses to make those rules work. That machinery is the **lexical6 min readlet, const, and var. Here we answer one question that causes many closure bugs: wh5 min readsetTimeout calls, know the difference between var and let from the earlier chapter on block and function scope, and accept the previous chapter's main point: a closure captures a variable, not a copy of its value. Together, those two ideas explain a bug almost6 min readnew. Each call runs the function body again, so each call also creates a new lexical environment. Methods defined inside the factory close over that environment, so every object it returns gets 4 min readvar and let/const, and with the private state pattern from the earlier chapters, where a function returns methods that close over local variables. In that pattern you call the outer function wheneve5 min readlet and var differ inside loops. You should also be able to pass a function as an argument. All examples run in Node.js 16 or later. The event examples use EventTarget, which 5 min read...args), spread syntax, and Function.prototype.apply. You will build two related techniques on top of closures. Partial application fixes5 min readMap, rest parameters (...args), and Function.prototype.apply. Memoization is the first pattern here where the closure's private variable does real work.4 min readthis inside object methods and constructors, and that you know what call, apply, and bind do in general.4 min read