Created
September 3, 2019 03:42
-
-
Save MrNanosh/936cac53bbe2042b6142c5b4c2c2662a to your computer and use it in GitHub Desktop.
assignment solution for https://courses.thinkful.com/js-v1/checkpoint/11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
What is scope? Your explanation should include the idea of global vs. block scope. | |
Scope is realm of influence of a variable. So long as var is avoided the only two scopes to be concerned with are global and block. variables declared with either let or const can exhibit both types of scope. Generally a variable declared in either way is defined within the block where it is declared and by extension any of the blocks nested within like russian dolls. Blocks are between curly "{}" braces. Any variable that is redefined within an inner block without being re-instantiated will be redefined for the outter block as well. | |
Global variables are defined and instantiated outside of any function and can be seen by even other javascript files that are loaded after the original. This means that they are defined and incidentally re-definable in every other block that is loaded after! | |
Why are global variables avoided? | |
Global files should be avoided because of the general problem of unintended concequences. Glbal variables may be redefined in an unintended way, and in doing so change the behaviour of another block of code unexpectedly. This is a debugging nightmare that grows with the size of the application. Globally scoped variables can be redefined by your own code or even that of another library, making trying to figure out what has happened really difficult. Avoiding globally scoped variables will prevent at least one source of errors. | |
Explain JavaScript's strict mode. | |
JavaScript has a strict mode that is declared by prefacing files with "'use strict';". Under this rule, if any variable is defined in an inner scope without being instantiated then an error is thrown. This way globally scoped variables can't be changed without alerting the developer. | |
What are side effects, and what is a pure function? | |
A function is written to carry out a set of instructions and as long as it does that without doing anything unintended on the outside of it's function block then it is considered to be pure. It is deterministic in that it does what those sets of instructions tell it to do and nothing else. This means that it doesn't influence other code written elsewhere; this would be a side effect. And it similarly isn't affected by code written elsewhere that isn't written to interact with the code in the function. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment