Lab 04: Closures & Scope
Overview
Step 1: Lexical Scope
const globalVar = 'global';
function outer() {
const outerVar = 'outer';
function inner() {
const innerVar = 'inner';
// inner has access to outer and global scope
console.log(globalVar); // 'global'
console.log(outerVar); // 'outer'
console.log(innerVar); // 'inner'
}
inner();
// console.log(innerVar); // ReferenceError — not accessible here
}
outer();
// Block scope (let/const)
{
let blockScoped = 'block';
const alsoBlock = 'also block';
console.log(blockScoped); // 'block'
}
// console.log(blockScoped); // ReferenceErrorStep 2: Closures
Step 3: IIFE (Immediately Invoked Function Expression)
Step 4: Module Pattern
Step 5: Factory Functions
Step 6: Closure Pitfalls & Fixes
Step 7: WeakRef for Memory-Safe Closures
Step 8: Capstone — Memoization with Closures
Summary
Concept
Description
Example
Last updated
