Skip to content

Instantly share code, notes, and snippets.

@Schmerb
Created May 24, 2017 23:11
Show Gist options
  • Save Schmerb/1189bffb034a293d88eaac09c44aa4f2 to your computer and use it in GitHub Desktop.
Save Schmerb/1189bffb034a293d88eaac09c44aa4f2 to your computer and use it in GitHub Desktop.
Lesson 5 challenge, explaining in my own words
What is scope? (global / local)
Essentially, scope is about the different perspectives for different places in code, i.e. what variables and associated values other functions and variables can "see" or access. Global scope encompases the entirety of source code and even spans across files given the suffienct linking of such files. A variable defined in the global scope will be able to be accessed by anywhere else in the code, including inside functions and other files. Local scope refers to the perspective from inside a function, nested function, or bracketed area of code. Any variables declared inside of a function or nested function are not necessarily accessible in other functions. The scope chain comes into play here. Global scope is at the very top and local scopes exist below it. Variables can only be accessed in a desceding manner in regards to the chain. A variable declared inside a function locally, will not be able to be accessed from places outside of the function, or up the scope chain, but areas down the scope chain -- nested functions -- can access these variables defined locally to their parent function.
Why avoid global vars?
We try to avoid global variables when possible due to unintended side effects and indeterminate functions that come as a result of using globals. One function may set the global variable to a certain value, but another function may inadvertently change the very same variable. As programs become more and more complex, this can make bug fixes very tedious.
JS' strict mode
By adding 'use strict'; either at the beginning of a function or even an entire file, it ensures that no global variables will be created. If one were to be declared, an uncaught reference error will be thrown to alert that the code attempted to create a global var and not let it go unnoticed.
Side effects occur when a function reaches outside of its own scope to create a variable, mutate pre-existing variables or interact with something, such as console.log(); A pure function is one that produces no side-effects and given the same input, it will produce the same result each time (determinate)
Unlike some othe languages, JavaScript relies on a method knbown as hoisting at run time. As code is executed, an initial "pass" of the code happens from top to bottom, left to right, making note of all variable and function declarations in order to store appropriate spaces in memory for these elements. As a result, the order of functions and their associated calls does not matter since a reference to such functions exist even if the function declaration comes after the call to the very function in the code. Variables get the value of undefined as well as function expressions; however, function declarations get the code between the following brackets stored during that pass so their value is in fact the function itself, thus allowing function calls that preceed function declarations to take place and execute as if the call had come after the declaration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment