Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Scope is the tiered system by which variables can be accessed in a program. When a variable is called for from
- inside a function, the program will look for that variable first within the function, then withing the parent function
- if not found, and so on until it finds the variable. Variables contained withing function are said to have local
- scope, as they can only be called withing that function. On the other hand, varibales that are declared outside of any
- function are are said to have global scope, as they can be called and manipulated from anywhere withing the program,
- even across files.
- Variables with global scope ('globals') are especially tricky when considering 'hoisting', which refers to the way
- in which JavaScript program variables are declared within their scope before the pogram is run. If a global variable is
- being declared within a function, it coud be altered from within other functions before the program even runs. Global
- varibales are best avoided, becuase of their open accessability they can be tricky to manage and changing them could
- have unintended side effects.
- Unintended side effects happen when the programmer creates a variable within function that has the same name as a
- global variable and doesn't use the 'var' keyword to signify that it is separate. This causes the function to reach
- outside of it's scope and alter the global variable, thereby rendering the program indeterminate. A function is
- considered 'pure' when it is determinate and has no side effects. JavaScript offeres a useful tool to help programmers
- avoid unintended side effects called 'strict' mode. When 'strict' is activated, it raises an error whenever the user
- 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