What is Scope ?
Scoping itself is how you search for a variable with a given name.
A variable has a scope which is the whole area in which that variable can be accessed by name.
How does scope work in JavaScript ?
In Javascript, we can call Scope as the set of rules that govern
how the Engine can look up a variable by its identifier name and find it.
What is call-stack in JavaScript ?
- A mechanism for an interpreter to keep track of its place in a script that calls multiple functions.
- Knows what function is currently being run and what functions are called from within that function.
- Stack data structure is LIFO that means, last in first out.
What is author-time ?
- This is the time that when developer writes the code.
- This can be taken to mean, perception of program as you see it before compilation.
Dynamic scope
- In dynamic scoping, you search in the local function first,
- Then you search in the function that called the local function,
- Then you search in the function that called that function,
- And so on, up the call-stack.
- Scope can be determined dynamically at run-time
Lexical scope (static scope)
- Based on where variables and blocks of scope are authored, by you, at write-time
- The scope of an identifier is fixed to some region in the source code containing the identifier’s declaration
- Scope can be determined at author-time
Conclusion
- Lexical scope is write-time, whereas dynamic scope is run-time
- Lexical scope care where a function was declared, but dynamic scope cares where a function was called from.
Additional Knowledge
Dynamic scoping and Lexical scoping
Call-stack
You don’t know JS, Scope&Closures by Kyle Simpson