Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. Scope is the tiered system by which variables can be accessed in a program. When a variable is called for from
  2. inside a function, the program will look for that variable first within the function, then withing the parent function
  3. if not found, and so on until it finds the variable. Variables contained withing function are said to have local
  4. scope, as they can only be called withing that function. On the other hand, varibales that are declared outside of any
  5. function are are said to have global scope, as they can be called and manipulated from anywhere withing the program,
  6. even across files.
  7.  
  8. Variables with global scope ('globals') are especially tricky when considering 'hoisting', which refers to the way
  9. in which JavaScript program variables are declared within their scope before the pogram is run. If a global variable is
  10. being declared within a function, it coud be altered from within other functions before the program even runs. Global
  11. varibales are best avoided, becuase of their open accessability they can be tricky to manage and changing them could
  12. have unintended side effects.
  13.  
  14. Unintended side effects happen when the programmer creates a variable within function that has the same name as a
  15. global variable and doesn't use the 'var' keyword to signify that it is separate. This causes the function to reach
  16. outside of it's scope and alter the global variable, thereby rendering the program indeterminate. A function is
  17. considered 'pure' when it is determinate and has no side effects. JavaScript offeres a useful tool to help programmers
  18. avoid unintended side effects called 'strict' mode. When 'strict' is activated, it raises an error whenever the user
  19. declares a variable without the 'var' keyword, the one surefire way to prevent unintedned side effects.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement