Global Scope
Variable Scope¶
scope none 1. the extent of the area or subject matter that something deals with or to which it is relevant.we widened the scope of our investigation | such questions go well beyond the scope of this book.
Thesaurus: the scope of the investigation, EXTENT, range, breadth, width, reach, sweep, purview, span, horizon; area, sphere, field, realm, compass, orbit, ambit, terms/field of reference, jurisdiction; confine, limit; gamut.
Naming Conflicts¶
-
It's sometimes hard to come up with good variables names for your projects. However, having clear, well thought-out variable names (and function names) is crucial for the long-term success of an application. Remember, as a software developer, you will have to return to the application again and again over the years, to accomplish enhancements.
-
In the early days of computer programming two limitations made naming very hard. First, early programming languages set the maximum length of a variable name to very small--limits of six or eight characters were typical. Then, when we made a variable we had to make sure that the names we picked were not the same as any other names in programs at our site.
-
This resulted in variable names like these. The first three letters were an abbreviation for the overall system and the rest were an abbreviation of what it was. Can you see what they are?
TCSTLTAX
MMSTRSRC
CDPPNAME
-
All this because programs could see all the variables!
Variable Scope to the Rescue!¶
There is a solution to this in all modern languages. Today there is more than one scope for variables. Let's first talk about the scope where variables are available everywhere.
Global Scope¶
Variables that exist throughout an entire JavaScript file are called Global Variables. Their values can be changed anywhere in the code and even by other functions. What are the advantages and disadvantages of this?
New Coding Standard
Demos¶
Demos
- Demo: Global Scope]
- unit04/demos/demo-global-scope.html
Read more¶
Related Reading
Pages 85 - 88
Hands on Work¶
Labs
- Lab10: Global Scope
- unit04/labs/lab-10-global-scope.html