Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. /*
  2. I'm not quite sure why we're using gist for this challenge, which doesn't involve writing code, but anyway here we go.
  3.  
  4. "What is scope? Your explanation should include the idea of global vs. local scope."
  5. Scope describes the range of code for which a particular variable or constant is defined. A variable with local scope is defined inside a function (or object, or maybe something else I don't know about yet), and it can only be used inside that function. A global variable is declared/defined in the mainline text of a code file (or, if not using "strict" Javascript, defined without first being declared), and can be used anywhere in the file, or even in a separate but linked file.
  6. When both a local and global variable, with the same name, are available the local one takes precedence. That means any time you get or set the value, you get only the local one, and the global one is unaffected. When you leave the local scope, the global one will still have the same value it had before.
  7.  
  8. "Why are global variables avoided?"
  9. They're hard to keep track of, and often create bugs and unintended side effects, especially in larger programs. When you have a global variable, it can be changed by any piece of your code anywhere, which makes it hard to find the problems. One function may accidentally change the behavior of another function by affecting the variable's value. You have to know when you're coding one part of your program whether a certain variable name was used anywhere else. With local variables, in contrast, it doesn't matter if you pick a variable name that has already been used somewhere else, because only one is in the local scope at a time.
  10.  
  11. "Explain JavaScript's strict mode."
  12. Many Javascript interpreters are lenient, guessing or compensating for certain syntax errors a programmer might make. In particular, if you try to define a variable before you declare it, the interpreter will usually store it as a global variable. This might keep your code functioning, but it leads to accidental global variables and the problems listed above. Under strict mode, Javascript will simply raise an error when this happens. If you did this by accident, you'll be forced to fix it before it gets to the user, and before it causes problems later which will be much harder to debug.
  13.  
  14. "What are side effects, and what is a pure function?"
  15. Side effects occur when a function changes a value that exists outside the function itself. Some functions might intentionally create side effects, like one that sorts a list you pass it — changing the original rather than returning a sorted copy. Often, though, side effects can be unintended and break other functions in your code. If two functions rely on the same global variable, either one can break the other.
  16. "Pure" functions didn't come up in the Thinkful chapter at all, but like any good developer, I googled it. A pure function always returns the same value for the same input. It doesn't rely on any external state (like a global variable), and doesn't produce side effects (explained above.)
  17.  
  18. "Explain variable hoisting in JavaScript."
  19. Hoisting refers to the way the Javascript interpreter scans the code for variables and functions, setting aside space for each, before running the code line-by-line. In effect, it's as though you declared (but not defined) all those variables at the beginning. If you use a variable earlier in the code than where you wrote the declaration and definition, it will come up as undefined but will not throw an error (as it would for a variable that doesn't exist at all.) If you use a function before you declare it, it runs perfectly, as though you had declared it at the beginning.
  20.  
  21.  
  22. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement