Closure
الإغلاق (Closure)
Definition
A function that remembers and has access to variables from its outer scope, even after that scope has closed.
دالة تتذكر وتصل إلى المتغيرات من نطاقها الخارجي، حتى بعد إغلاق ذلك النطاق.
Why It Matters
The stale closure bug is one of the most common React mistakes — useEffect running with an outdated value because it captured an old copy via closure.
خطأ الإغلاق القديم هو أحد أكثر أخطاء React شيوعًا — useEffect يعمل بقيمة قديمة لأنه التقط نسخة قديمة عبر الإغلاق.
Full Definition
Example Usage
“function makeCounter() { let count = 0; return () => ++count; } const counter = makeCounter(); counter(); // 1, counter(); // 2”
“function makeCounter() { let count = 0; return () => ++count; } const counter = makeCounter(); counter(); // 1، counter(); // 2”
AI Builder Tips
Avoid these mistakes when using Closure:
Stale closures in useEffect — forgetting to add dependencies to the dependency array
Capturing loop variables in closures — use let not var
Sign in to unlock guided AI explanations from AI Teacher.
Generate a Prompt
Copy this prompt and use it directly with any AI model — no setup needed.
Help me build a project using Closure. Explain: 1. What is Closure and why it matters 2. The core architecture and required tools 3. Step-by-step implementation plan 4. Common mistakes to avoid: Stale closures in useEffect — forgetting to add dependencies to the dependency array, Capturing loop variables in closures — use let not var 5. Best practices and production tips