You Don't Know JS (Scopes and Closures) - Getify

👩‍💻👩‍💻 You Don't Know JS : Scopes & Closures🔒🎉 ➡ 1 (Intro) #

- 3 steps of compilation: (1️⃣ Tokenizing 🥇 : converts characters to chunks called tokens 2️⃣ Parsing ➰ 🌲: Converts tokens into AST, 3️⃣ Code Generation 🧬: Converts AST into machine code)

- Targets vs sources 🎯🥧, targets are identifier/names🎯, sources🥧 are the values, many different ways of target to value assertion, eg function getStudentName(..) , where getStudentName🎯 is a target and function(..) 🥧is a source .

- NEVER USE eval() 👎🏻🚫 => can mess with the scope ➿ already established at runtime, and also takes a performance hit. For example

- with() keyword is also bad

- Scopes determined at compilation, Use the bucket and marble 🥃🎱 metaphor to match a variable with a scope. (same color marbles go in same color buckets)

👩‍💻👩‍💻 You Don't Know JS : Scopes & Closures🔒🎉 ➡ 2 (Engine, Compiler and Scope manager) #

- Engine🚂 : handles compilation to execution(start to finish), Compiler: 📄 Handles compilation(tokenization ⏭ AST ⏭ machine code), Scope manager 👨🏻‍💼: Keeps track of variables and scopes

- What happens when var studentArray = [ ... ] //initialization

  1. (During compilation 📄💨) Compiler asks ❓ if scope manager has a studentArray declaration, if not declares it
  2. (During runtime) Engine asks ❓ scope manager if studentArray is variable, says yes then Engine initializes it to undefined so its ready to use 🏁 , and then assigns the array value to it.

- When JS engine comes across a variable, asks current scope for value, if not , it asks the outer scope, and so on...

- 'var' temporarily sets to undefined,(unlike let/const which sets it uninitialized / TDZ 🔪)

- Says reference error 🤯 if source lookup fails (get(x)) or target(x=2 where x has not been declared)lookup fails(in strict mode)

- Variable scopes are usually determined at compilation ➿ before runtime🏃🏻‍♂️, unless a variable cannot be found in the current file (exists in a different file). In that case, it is resolved at runtime( when it finds it in a different file) (resolved to global scope usually)