Remember the Book: Functional JavaScript
So chapter 3 right?…hm
Scopes
From my understanding, where a variable lives, while the extent of a scope means how long a variable holds a value.
Global Scope
- This has the longest lifespan
- when you declare a variable without the var, you are defining a global variable (accessible to every function and method in the program)
Lexical Scope
- refers to the visibility of a variable and its value
- If I understood correctly, the closest variable value takes precedence
Dynamic Scope
- The value at the top of the stack in a binding is the current value
- The poison of dynamic scoping (quote): the value of any given binding cannot be known until the caller of any function is known
This reference
- Can point to different values depending on the context in which it was first created
- but it is also determined by the caller
- can be manipulated through the use of apply or call
- the referenced object, is whatever object is passed into them as the first argument
- powerful but dangerous
- with great power comes great.. oooh pizza!
- using underscore’s _.bind method can lock the this reference from changing
Function Scope
- each function can refer to a this reference
- in JS, all var declarations in a function body are implicitly moved to the top of the function in which they occur
- rearrangement of variable declarations in Javascript is called hoisting
- Any code in the function can see al the variables defined inside ergh…
Closures
- a function that captures values near where it was created
- when it captures a value, the value’s lifespan can be extended
- not only local variables but also arguments can be captured as well
- they can see all the variables declared within the function they live in
Free variables
- related to closures
- are easily captured by inner functions
Shadowing
- occurs in Js when a variable of a name y is declared within a certain scope and then another variable od the same name is declared in a lower scope
- here the innermost value takes precedence
Basically,
Closures provide protection and private access when used right
See the Pen Functional Javascript: Scope by Ajala Comfort (@AJALACOMFORT) on CodePen.
So that is all for today, if you want detailed explanation of the topic / chapter, do purchase the book. See ya