update post: Closure and this - How are variables resolved within functions?

This commit is contained in:
2020-08-03 09:28:20 +05:30
parent c9fae70df3
commit 8b65418610
+4 -2
View File
@@ -113,9 +113,9 @@ a. it will check in the current LexicalEnvironment (i.e. the new local scope) fi
b. if it doesn't find the variable there, it will check in it's parent scope (parent of LE = F.[[Environment]] = lexical/closure scope),
c. and so on.
This lookup will finally end when it reaches the global scope (doesn't have parent scope). If the variable is still not found, it will throw a `ReferenceError`.
This lookup will finally end when it reaches the global scope (which doesn't have any parent scope). If the variable is still not found, it will throw a `ReferenceError`.
If you remember the scope chain of `inner` func, this means that the variable will never be looked up in caller scope (scopeC).
If you remember the scope chain of `inner` func, this means that the variable will be looked up in lexical/creation scope (scopeA) and NEVER in caller scope (scopeC). This behavior is called as closure or lexical scoping.
Even if there is no definition for `myText` in the lexical scope (scopeA), it will NOT use the value from scopeC - and indeed throw a error. Example ⤵️
```js
@@ -136,6 +136,8 @@ function outer() {
}
```
(Actual implementations of closure in different engines will optimize the closure by only storing variables that are actually being used (if any). But the Ecmascript specification doesn't talk about those optimizations and tells you to directly store the whole scope, for all functions.)
## `this` lookup
Earlier, we looked at how normal variables get resolved in a function. But whenever you write `this.someThing`, the value of `this` does NOT get resolved in the same way. It is called as a `ThisExpression`, which gets resolved specially.